1use axum::{
7 Json,
8 http::StatusCode,
9 response::{IntoResponse, Response},
10};
11use serde::Serialize;
12use thiserror::Error;
13
14#[derive(Error, Debug)]
17pub enum ApiError {
18 #[error("bad request: {0}")]
19 BadRequest(String),
20 #[error("not found")]
21 NotFound,
22 #[error("internal server error")]
23 InternalServerError,
24}
25
26impl IntoResponse for ApiError {
27 fn into_response(self) -> Response {
28 let status_code = match self {
29 ApiError::BadRequest(_) => StatusCode::BAD_REQUEST,
30 ApiError::NotFound => StatusCode::NOT_FOUND,
31 ApiError::InternalServerError => StatusCode::INTERNAL_SERVER_ERROR,
32 };
33
34 let body = Json(ErrorResponse {
35 error_code: status_code.as_u16().to_string(),
36 error_message: self.to_string(),
37 });
38
39 (status_code, body).into_response()
40 }
41}
42
43#[derive(Clone, Debug, Serialize)]
45pub(crate) struct ErrorResponse {
46 error_code: String,
47 error_message: String,
48}