Files
buzz/crates/buzz-relay/src/api/admin/error.rs
Kalvin C 68e670e001 feat: add read-only deployment moderation dashboard (#1999)
Signed-off-by: npub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7 <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1ntr8jjcqq6gt06q5avvqttfjgpwshmra22pcmcagdnukw4ja4nqqsa9g54 <9ac6794b000690b7e814eb1805ad32405d0bec7d52838de3a86cf967565dacc0@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7 <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@sprout-oss.stage.blox.sqprod.co>
2026-07-17 12:50:06 -07:00

83 lines
1.7 KiB
Rust

use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde::Serialize;
#[derive(Debug)]
pub struct ApiError {
pub status: StatusCode,
pub code: &'static str,
pub message: &'static str,
}
#[derive(Serialize)]
struct ErrorEnvelope {
error: ErrorBody,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ErrorBody {
code: &'static str,
message: &'static str,
request_id: uuid::Uuid,
}
impl ApiError {
pub fn bad_request(code: &'static str, message: &'static str) -> Self {
Self {
status: StatusCode::BAD_REQUEST,
code,
message,
}
}
pub fn forbidden() -> Self {
Self {
status: StatusCode::FORBIDDEN,
code: "forbidden",
message: "request is not authorized",
}
}
pub fn not_found() -> Self {
Self {
status: StatusCode::NOT_FOUND,
code: "not_found",
message: "record was not found",
}
}
pub fn internal() -> Self {
Self {
status: StatusCode::INTERNAL_SERVER_ERROR,
code: "internal_error",
message: "request failed",
}
}
}
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
(
self.status,
Json(ErrorEnvelope {
error: ErrorBody {
code: self.code,
message: self.message,
request_id: uuid::Uuid::new_v4(),
},
}),
)
.into_response()
}
}
impl From<buzz_db::DbError> for ApiError {
fn from(_: buzz_db::DbError) -> Self {
Self::internal()
}
}