use std::borrow::Cow;
use crate::entity::{ApiError, ApiErrorDetail};
#[derive(Debug)]
pub struct ApiErrorResponse {
pub status_code: axum::http::StatusCode,
pub body: ApiError,
}
impl ApiErrorResponse {
pub const fn internal() -> Self {
Self {
status_code: axum::http::StatusCode::INTERNAL_SERVER_ERROR,
body: ApiError {
message: Cow::Borrowed("internal error"),
detail: None,
},
}
}
pub fn bad_request(message: impl Into<Cow<'static, str>>) -> Self {
Self {
status_code: axum::http::StatusCode::BAD_REQUEST,
body: ApiError {
message: message.into(),
detail: None,
},
}
}
pub fn not_found(message: impl Into<Cow<'static, str>>) -> Self {
Self {
status_code: axum::http::StatusCode::NOT_FOUND,
body: ApiError {
message: message.into(),
detail: None,
},
}
}
pub fn conflict(message: impl Into<Cow<'static, str>>) -> Self {
Self {
status_code: axum::http::StatusCode::CONFLICT,
body: ApiError {
message: message.into(),
detail: None,
},
}
}
pub fn unauthorized(message: impl Into<Cow<'static, str>>) -> Self {
Self {
status_code: axum::http::StatusCode::UNAUTHORIZED,
body: ApiError {
message: message.into(),
detail: None,
},
}
}
pub fn with_detail(mut self, detail: ApiErrorDetail) -> Self {
self.body.detail = Some(detail);
self
}
}