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::{
|
use axum::{
|
||||||
extract::rejection::JsonRejection,
|
extract::rejection::JsonRejection,
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
|
@ -18,29 +16,21 @@ pub const ERR_NOT_FOUND: ApiError<'static> = ApiError::ClientError {
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum ApiError<'a> {
|
pub enum ApiError<'a> {
|
||||||
|
#[error("client error: <{code}> {message}")]
|
||||||
ClientError {
|
ClientError {
|
||||||
status: StatusCode,
|
status: StatusCode,
|
||||||
code: &'a str,
|
code: &'a str,
|
||||||
message: &'a str,
|
message: &'a str,
|
||||||
},
|
},
|
||||||
InternalError(#[from] anyhow::Error),
|
|
||||||
DatabaseError(#[from] sqlx::Error),
|
|
||||||
JSONFormatError(#[from] JsonRejection),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for ApiError<'_> {
|
#[error("unexpected internal error: {0}")]
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
InternalError(#[from] anyhow::Error),
|
||||||
match self {
|
|
||||||
ApiError::ClientError {
|
#[error("database returnede error: {0}")]
|
||||||
status,
|
DatabaseError(#[from] sqlx::Error),
|
||||||
code,
|
|
||||||
message,
|
#[error("incoming JSON format error: {0}")]
|
||||||
} => write!(f, "Client error: {} {}: {}", status, code, message),
|
JSONFormatError(#[from] JsonRejection),
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse for ApiError<'_> {
|
impl IntoResponse for ApiError<'_> {
|
||||||
|
|
Loading…
Reference in a new issue