1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::fmt::Display;
use serde::Serialize;
use crate::{dtos::message::MessageResource, enums::error::Error, extensions::{typed_response::TypedHttpResponse, generic_error::GenericError}};
pub trait ReturnableErrorShape {
fn convert_to_returnable<T: Serialize> (&self, status_code: u16) -> TypedHttpResponse<T>;
}
impl ReturnableErrorShape for MessageResource {
fn convert_to_returnable<T: Serialize>(&self, status_code: u16) -> TypedHttpResponse<T> {
TypedHttpResponse::return_standard_error(status_code, self.clone())
}
}
impl ReturnableErrorShape for Error {
fn convert_to_returnable<T: Serialize>(&self, status_code: u16) -> TypedHttpResponse<T> {
match self {
Error::Unspecified => TypedHttpResponse::return_standard_error(status_code, MessageResource::new_empty()),
Error::NetworkError(message) => TypedHttpResponse::return_standard_error(status_code, message.clone()),
Error::UnexpectedStatusCode(_, actual, messages) => TypedHttpResponse::return_standard_error_list(*actual, messages.clone()),
Error::ClientError(message) => TypedHttpResponse::return_standard_error(status_code, message.clone()),
Error::SerdeError(message, _) => TypedHttpResponse::return_standard_error(status_code, message.clone()),
Error::DatabaseError(message, _) => TypedHttpResponse::return_standard_error(status_code, message.clone()),
Error::ComputeError(message) => TypedHttpResponse::return_standard_error(status_code, message.clone()),
}
}
}
impl ReturnableErrorShape for Vec<MessageResource> {
fn convert_to_returnable<T: Serialize>(&self, status_code: u16) -> TypedHttpResponse<T> {
TypedHttpResponse::return_standard_error_list(status_code, self.to_vec())
}
}
impl<E: Display> ReturnableErrorShape for GenericError<E>{
fn convert_to_returnable<T: Serialize>(&self, status_code: u16) -> TypedHttpResponse<T> {
TypedHttpResponse::return_standard_error(status_code, MessageResource::new_from_str(&self.error.to_string()))
}
}