This commit is contained in:
parent
dd45fd26f3
commit
33b743f213
1 changed files with 9 additions and 19 deletions
|
@ -1,5 +1,3 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use axum::{
|
||||
extract::rejection::JsonRejection,
|
||||
http::StatusCode,
|
||||
|
@ -18,29 +16,21 @@ pub const ERR_NOT_FOUND: ApiError<'static> = ApiError::ClientError {
|
|||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ApiError<'a> {
|
||||
#[error("client error: <{code}> {message}")]
|
||||
ClientError {
|
||||
status: StatusCode,
|
||||
code: &'a str,
|
||||
message: &'a str,
|
||||
},
|
||||
InternalError(#[from] anyhow::Error),
|
||||
DatabaseError(#[from] sqlx::Error),
|
||||
JSONFormatError(#[from] JsonRejection),
|
||||
}
|
||||
|
||||
impl Display for ApiError<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
ApiError::ClientError {
|
||||
status,
|
||||
code,
|
||||
message,
|
||||
} => write!(f, "Client error: {} {}: {}", status, code, message),
|
||||
ApiError::InternalError(err) => write!(f, "Unexpected error: {}", err),
|
||||
ApiError::DatabaseError(err) => write!(f, "Database error: {}", err),
|
||||
ApiError::JSONFormatError(err) => write!(f, "JSON format error: {}", err),
|
||||
}
|
||||
}
|
||||
#[error("unexpected internal error: {0}")]
|
||||
InternalError(#[from] anyhow::Error),
|
||||
|
||||
#[error("database returnede error: {0}")]
|
||||
DatabaseError(#[from] sqlx::Error),
|
||||
|
||||
#[error("incoming JSON format error: {0}")]
|
||||
JSONFormatError(#[from] JsonRejection),
|
||||
}
|
||||
|
||||
impl IntoResponse for ApiError<'_> {
|
||||
|
|
Loading…
Reference in a new issue