macros are nice sometimes
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Hamcha 2023-07-06 12:37:55 +02:00
parent dd45fd26f3
commit 33b743f213
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89

View file

@ -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<'_> {