mmtickets_common/middlewares/
error_handler.rsuse axum::response::IntoResponse;
use hyper::StatusCode;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
struct InnerErrorMessage(String);
#[derive(Debug, Deserialize, Serialize)]
pub struct InnerError {
errors: Vec<InnerErrorMessage>,
}
impl InnerError {
pub fn new(message: String) -> Self {
Self {
errors: vec![InnerErrorMessage(message)],
}
}
pub fn add(&mut self, message: String) {
self.errors.push(InnerErrorMessage(message));
}
}
impl ToString for InnerError {
fn to_string(&self) -> String {
serde_json::to_string(&self).unwrap()
}
}
pub struct CommonError {
pub status: StatusCode,
pub message: InnerError,
}
impl CommonError {
pub fn new(status: StatusCode, message: String) -> Self {
Self {
status,
message: InnerError::new(message),
}
}
pub fn add(&mut self, message: String) {
self.message.add(message);
}
}
impl IntoResponse for CommonError {
fn into_response(self) -> axum::response::Response {
(self.status, self.message.to_string()).into_response()
}
}
impl std::fmt::Debug for CommonError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"CommonError {{ status: {:?}, message: {:?} }}",
self.status, self.message
)
}
}
impl std::fmt::Display for CommonError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"CommonError {{ status: {:?}, message: {:?} }}",
self.status, self.message
)
}
}