51 lines
1.4 KiB
Rust
51 lines
1.4 KiB
Rust
|
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 serde::Serialize;
|
||
|
use crate::error::{APIError, APIErrorsResponse};
|
||
|
|
||
|
pub struct OkResponse<T: Responder>(T);
|
||
|
|
||
|
#[derive(Debug)]
|
||
|
pub struct ErrResponse(APIErrorsResponse);
|
||
|
|
||
|
impl<T: Responder> Responder for OkResponse<T> {
|
||
|
type Body = T::Body;
|
||
|
|
||
|
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body> {
|
||
|
self.0.respond_to(req)
|
||
|
}
|
||
|
}
|
||
|
impl Responder for ErrResponse {
|
||
|
type Body = EitherBody<String>;
|
||
|
|
||
|
fn respond_to(self, req: &HttpRequest) -> HttpResponse<Self::Body> {
|
||
|
Json(self.0).respond_to(req)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl From<DbErr> for ErrResponse {
|
||
|
fn from(value: DbErr) -> Self {
|
||
|
error!("database error: {}", value);
|
||
|
Self {
|
||
|
0: 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 {}
|