2025-08-10 00:25:29 +00:00
|
|
|
use axum::{
|
|
|
|
|
http::StatusCode,
|
|
|
|
|
response::{IntoResponse, Response},
|
|
|
|
|
};
|
2024-06-16 21:56:14 +09:00
|
|
|
use std::fmt;
|
|
|
|
|
|
2025-08-10 00:25:29 +00:00
|
|
|
// todo: model parse errors
|
2024-06-16 21:56:14 +09:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct PKError {
|
|
|
|
|
pub response_code: StatusCode,
|
|
|
|
|
pub json_code: i32,
|
|
|
|
|
pub message: &'static str,
|
2025-08-10 00:25:29 +00:00
|
|
|
|
|
|
|
|
pub inner: Option<anyhow::Error>,
|
2024-06-16 21:56:14 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for PKError {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
write!(f, "{:?}", self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-10 00:25:29 +00:00
|
|
|
impl Clone for PKError {
|
|
|
|
|
fn clone(&self) -> PKError {
|
|
|
|
|
if self.inner.is_some() {
|
|
|
|
|
panic!("cannot clone PKError with inner error");
|
|
|
|
|
}
|
|
|
|
|
PKError {
|
|
|
|
|
response_code: self.response_code,
|
|
|
|
|
json_code: self.json_code,
|
|
|
|
|
message: self.message,
|
|
|
|
|
inner: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<E> From<E> for PKError
|
|
|
|
|
where
|
|
|
|
|
E: std::fmt::Display + Into<anyhow::Error>,
|
|
|
|
|
{
|
|
|
|
|
fn from(err: E) -> Self {
|
|
|
|
|
let mut res = GENERIC_SERVER_ERROR.clone();
|
|
|
|
|
res.inner = Some(err.into());
|
|
|
|
|
res
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoResponse for PKError {
|
|
|
|
|
fn into_response(self) -> Response {
|
|
|
|
|
if let Some(inner) = self.inner {
|
|
|
|
|
tracing::error!(?inner, "error returned from handler");
|
|
|
|
|
}
|
|
|
|
|
crate::util::json_err(
|
|
|
|
|
self.response_code,
|
|
|
|
|
serde_json::to_string(&serde_json::json!({
|
|
|
|
|
"message": self.message,
|
|
|
|
|
"code": self.json_code,
|
|
|
|
|
}))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macro_rules! fail {
|
|
|
|
|
($($stuff:tt)+) => {{
|
|
|
|
|
tracing::error!($($stuff)+);
|
|
|
|
|
return Err(crate::error::GENERIC_SERVER_ERROR);
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) use fail;
|
2024-06-16 21:56:14 +09:00
|
|
|
|
|
|
|
|
macro_rules! define_error {
|
|
|
|
|
( $name:ident, $response_code:expr, $json_code:expr, $message:expr ) => {
|
2025-08-10 00:25:29 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
|
pub const $name: PKError = PKError {
|
2024-06-16 21:56:14 +09:00
|
|
|
response_code: $response_code,
|
|
|
|
|
json_code: $json_code,
|
|
|
|
|
message: $message,
|
2025-08-10 00:25:29 +00:00
|
|
|
inner: None,
|
2024-06-16 21:56:14 +09:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-24 11:43:05 -05:00
|
|
|
define_error! { GENERIC_AUTH_ERROR, StatusCode::UNAUTHORIZED, 0, "401: Missing or invalid Authorization header" }
|
2025-08-10 00:25:29 +00:00
|
|
|
define_error! { GENERIC_BAD_REQUEST, StatusCode::BAD_REQUEST, 0, "400: Bad Request" }
|
2026-01-24 11:43:05 -05:00
|
|
|
define_error! { GENERIC_NOT_FOUND, StatusCode::NOT_FOUND, 0, "404: Not Found" }
|
2025-08-10 00:25:29 +00:00
|
|
|
define_error! { GENERIC_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR, 0, "500: Internal Server Error" }
|