use axum::Json;
use axum::http::StatusCode;
use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct ApiError {
pub error: String,
pub code: String,
}
impl ApiError {
pub(crate) fn unauthorized() -> (StatusCode, Json<Self>) {
(
StatusCode::UNAUTHORIZED,
Json(Self {
error: "unauthorized".to_owned(),
code: "UNAUTHORIZED".to_owned(),
}),
)
}
pub(crate) fn unauthorized_with(msg: impl Into<String>) -> (StatusCode, Json<Self>) {
(
StatusCode::UNAUTHORIZED,
Json(Self {
error: msg.into(),
code: "UNAUTHORIZED".to_owned(),
}),
)
}
pub(crate) fn not_found(msg: impl Into<String>) -> (StatusCode, Json<Self>) {
(
StatusCode::NOT_FOUND,
Json(Self {
error: msg.into(),
code: "NOT_FOUND".to_owned(),
}),
)
}
pub(crate) fn conflict(msg: impl Into<String>) -> (StatusCode, Json<Self>) {
(
StatusCode::CONFLICT,
Json(Self {
error: msg.into(),
code: "CONFLICT".to_owned(),
}),
)
}
pub(crate) fn bad_request(msg: impl Into<String>) -> (StatusCode, Json<Self>) {
(
StatusCode::BAD_REQUEST,
Json(Self {
error: msg.into(),
code: "BAD_REQUEST".to_owned(),
}),
)
}
pub(crate) fn forbidden(msg: impl Into<String>) -> (StatusCode, Json<Self>) {
(
StatusCode::FORBIDDEN,
Json(Self {
error: msg.into(),
code: "FORBIDDEN".to_owned(),
}),
)
}
pub(crate) fn gone(msg: impl Into<String>) -> (StatusCode, Json<Self>) {
(
StatusCode::GONE,
Json(Self {
error: msg.into(),
code: "GONE".to_owned(),
}),
)
}
pub(crate) fn internal_error(msg: impl Into<String>) -> (StatusCode, Json<Self>) {
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(Self {
error: msg.into(),
code: "INTERNAL_ERROR".to_owned(),
}),
)
}
}