use std::fmt::{Display, Formatter}; use actix_web::{HttpRequest, HttpResponse, Responder, ResponseError}; use actix_web::body::EitherBody; use actix_web::web::Json; use log::error; use sea_orm::DbErr; use crate::error::{APIError, APIErrorsResponse}; pub struct OkResponse(T); #[derive(Debug)] pub struct ErrResponse(APIErrorsResponse); impl Responder for OkResponse { type Body = T::Body; fn respond_to(self, req: &HttpRequest) -> HttpResponse { self.0.respond_to(req) } } impl Responder for ErrResponse { type Body = EitherBody; fn respond_to(self, req: &HttpRequest) -> HttpResponse { Json(self.0).respond_to(req) } } impl From for ErrResponse { fn from(value: DbErr) -> Self { error!("database error: {}", value); Self(APIErrorsResponse { errors: vec![ APIError { code: "ERR_DB_ERROR".to_string(), message: "There was an error performing the database query. Please try again later.".to_string(), path: None, } ] }) } } impl Display for ErrResponse { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", self.0) } } impl ResponseError for ErrResponse {}