Added generic error wrapper

This commit is contained in:
Franklin 2022-08-31 14:18:00 -04:00
parent edc50440e4
commit 421111f880
5 changed files with 13 additions and 6 deletions

2
Cargo.lock generated
View File

@ -183,7 +183,7 @@ dependencies = [
[[package]]
name = "actix-web-utils"
version = "0.2.9"
version = "0.2.10"
dependencies = [
"actix-web",
"log",

View File

@ -1,6 +1,6 @@
[package]
name = "actix-web-utils"
version = "0.2.9"
version = "0.2.10"
edition = "2021"
authors = ["Franklin E. Blanco"]
description = "Just some useful addons for actix web."

View File

@ -0,0 +1,6 @@
use std::fmt::Display;
pub struct GenericError<E: Display> {
pub error: E
}

View File

@ -1,2 +1,3 @@
pub mod typed_response;
pub mod logger;
pub mod logger;
pub mod generic_error;

View File

@ -1,7 +1,7 @@
use std::fmt::Display;
use serde::Serialize;
use crate::{dtos::message::MessageResource, enums::error::Error, extensions::typed_response::TypedHttpResponse};
use crate::{dtos::message::MessageResource, enums::error::Error, extensions::{typed_response::TypedHttpResponse, generic_error::GenericError}};
/// This trait aims to aid macros defined in this crate so that the macro can take any shape of error and
/// do the same thing for all.
@ -32,8 +32,8 @@ impl ReturnableErrorShape for Vec<MessageResource> {
TypedHttpResponse::return_standard_error_list(status_code, self.to_vec())
}
}
impl<'a> ReturnableErrorShape for dyn Display + 'a {
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_err(self))
TypedHttpResponse::return_standard_error(status_code, MessageResource::new_from_str(&self.error.to_string()))
}
}