diff --git a/Cargo.lock b/Cargo.lock index 89eb8f4..949584e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -183,9 +183,10 @@ dependencies = [ [[package]] name = "actix-web-utils" -version = "0.2.20" +version = "0.2.21" dependencies = [ "actix-web", + "err", "log", "serde", "serde_json", @@ -387,6 +388,14 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "err" +version = "0.1.0" +source = "git+https://github.com/franklinblanco/err.git#93ff7e8e5cff0b3032e897a92ce583851cdeaf96" +dependencies = [ + "serde", +] + [[package]] name = "firestorm" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index a8279ab..f68181d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-web-utils" -version = "0.2.20" +version = "0.2.21" edition = "2021" authors = ["Franklin E. Blanco"] description = "Just some useful addons for actix web." @@ -14,4 +14,5 @@ repository = "https://github.com/franklinblanco/actix-web-utils" actix-web = "4.1.0" serde_json = { version = "1" } serde = { version = "1.0", features = ["derive"] } -log = { version = "0.4", features = ["serde"] } \ No newline at end of file +log = { version = "0.4", features = ["serde"] } +err = { git = "https://github.com/franklinblanco/err.git" } \ No newline at end of file diff --git a/src/dtos/message.rs b/src/dtos/message.rs deleted file mode 100644 index b3fcfd2..0000000 --- a/src/dtos/message.rs +++ /dev/null @@ -1,36 +0,0 @@ -use std::fmt::Display; - -use serde::{Serialize, Deserialize}; - -//TODO: Add examples -/// This is for sending errors back from requests conveniently. -/// This struct contains an optional key just in -/// case you want to deal with internationalization. -/// It was left as optional just in case you don't -/// have the time to yet... -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct MessageResource{ - pub key: Option, - pub message: String -} -impl Display for MessageResource { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "MessageResource Key: {:?}, Message: {}", self.key, self.message) - } -} - -impl MessageResource{ - pub fn new_empty() -> MessageResource{ - MessageResource { key: None, message: String::from("") } - } - pub fn new_from_str(msg: &str) -> MessageResource{ - MessageResource { key: None, message: String::from(msg) } - } - /// Just takes any error that implements display (Has a .to_string() method) - pub fn new_from_err(error: E) -> MessageResource{ - MessageResource { key: None, message: error.to_string() } - } - pub fn new(key: &str, msg: &str) -> MessageResource{ - MessageResource { key: Some(String::from(key)), message: String::from(msg) } - } -} \ No newline at end of file diff --git a/src/dtos/mod.rs b/src/dtos/mod.rs deleted file mode 100644 index 7397c12..0000000 --- a/src/dtos/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod message; \ No newline at end of file diff --git a/src/enums/error.rs b/src/enums/error.rs deleted file mode 100644 index f21adc5..0000000 --- a/src/enums/error.rs +++ /dev/null @@ -1,80 +0,0 @@ -use std::{fmt::{self}, str::FromStr}; - -use crate::dtos::message::MessageResource; - - -/// This is supposed to be used whenever you have an error in your code and want to be more specific about it. -/// Fits in with most CRUD web apps. What you send back to the client is a MessageResource, not the error itself! -#[derive(Debug, Clone)] -pub enum Error{ - /// Takes a Message and the query - DatabaseError(MessageResource, String), - /// Same as UnexpectedStatusCode but without the extra details. - ClientError(MessageResource), - /// Takes the status code you expected, the actual status code, and the ErrorMessage. This is meant to be used when your app tries to use an API, be it internal or external. - UnexpectedStatusCode(u16, u16, Vec), - /// Try and never use this error, unless you really need to. - Unspecified, - /// If you had an error serializing/deserializing and wish to display more details. Such as the entire Json as a string, this is how. - SerdeError(MessageResource, String), - /// Normally used in compute heavy operations, such as Hashing. - ComputeError(MessageResource), - /// Self explanatory, Network related error. - NetworkError(MessageResource) - -} -impl fmt::Display for Error{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *&self { - Error::Unspecified => write!(f, "Error of type Unspecified."), - Error::NetworkError(message) => write!(f, "Error of type Network.\nMessageResource: {}", message), - Error::UnexpectedStatusCode(expected, actual, messages) => write!(f, "Error of type UnexpectedStatusCode.\nExpected: {}\nActual: {}\nreceivedMessageResources: {:?}", expected, actual, messages), - Error::ClientError(message) => write!(f, "Error of type Client.\nMessageResource: {}", message), - Error::SerdeError(message, recieved) => write!(f, "Error of type Serialization/Deserialization.\nMessageResource: {:?}, Object attempted to be serded: {}", message, recieved), - Error::DatabaseError(message, query) => write!(f, "Error of type Database.\nMessageResource: {}, \nQuery: {}", message, query), - Error::ComputeError(message) => write!(f, "Error of type Compute.\nMessageResource: {}", message), - } - } -} -impl FromStr for Error { - type Err = Error; - - fn from_str(string: &str) -> Result { - let error_name_option = string.get(13..25); - let error_name_whole = match error_name_option { - Some(error_name_whole) => error_name_whole, - None => return Err(Error::Unspecified), - }; - if error_name_whole.starts_with("Unspecified") { - return Ok(Self::Unspecified) - } - if error_name_whole.contains("UnexpectedStatusCode") { - let expected_str_index = string.find("Expected: ").unwrap() + 10; - let actual_str_index = string.find("Actual: ").unwrap() + 8; - let expected_status_code = string.get(expected_str_index..expected_str_index+3).unwrap(); - let actual_status_code = string.get(actual_str_index..actual_str_index+3).unwrap(); - - let message_resources_string = string.get(string.find("receivedMessageResources").unwrap() + 26..string.len() - 1).unwrap(); - let message_resources: Vec = serde_json::from_str(message_resources_string).unwrap(); - return Ok(Self::UnexpectedStatusCode(expected_status_code.parse().unwrap(), actual_status_code.parse().unwrap(), message_resources)); - } - if error_name_whole.starts_with("Client") { - - } - if error_name_whole.starts_with("Network") { - - } - if error_name_whole.starts_with("Serialization") { - - } - if error_name_whole.starts_with("Database") { - - } - if error_name_whole.starts_with("Compute") { - - } - - Ok(Error::ClientError(MessageResource::new_from_str("msg"))) - } -} -impl std::error::Error for Error {} \ No newline at end of file diff --git a/src/enums/mod.rs b/src/enums/mod.rs index 16cc5d5..ed883f2 100644 --- a/src/enums/mod.rs +++ b/src/enums/mod.rs @@ -1,2 +1 @@ -pub mod error; pub mod macro_enums; \ No newline at end of file diff --git a/src/extensions/typed_response.rs b/src/extensions/typed_response.rs index ba5e7b0..9c0b7aa 100644 --- a/src/extensions/typed_response.rs +++ b/src/extensions/typed_response.rs @@ -1,8 +1,7 @@ use actix_web::{HttpResponse, http::StatusCode, web, HttpRequest, HttpResponseBuilder, body::BoxBody, Responder}; use serde::Serialize; -use crate::dtos::message::MessageResource; - +use err::MessageResource; /// Defines a type for actix web routes. As the current implementation of HttpResponse doesn't let you manually specify a type. /// ``` /// diff --git a/src/lib.rs b/src/lib.rs index 0f17331..16e48b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ pub mod extensions; -pub mod dtos; pub mod utils; pub mod enums; pub mod traits; \ No newline at end of file diff --git a/src/traits/macro_traits.rs b/src/traits/macro_traits.rs index c803832..b165c15 100644 --- a/src/traits/macro_traits.rs +++ b/src/traits/macro_traits.rs @@ -1,7 +1,8 @@ use std::fmt::Display; use serde::Serialize; -use crate::{dtos::message::MessageResource, enums::error::Error, extensions::{typed_response::TypedHttpResponse, generic_error::GenericError}}; +use err::{Error, MessageResource}; +use crate::{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. @@ -15,15 +16,14 @@ impl ReturnableErrorShape for MessageResource { } impl ReturnableErrorShape for Error { fn convert_to_returnable(&self, status_code: u16) -> TypedHttpResponse { - //debug!("Converted error to returnable. Error: {}", self.to_string()); 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::Unspecified => TypedHttpResponse::return_standard_error(status_code, MessageResource::from(self)), + Error::Network(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()), + Error::Serde(message) => TypedHttpResponse::return_standard_error(status_code, message.clone()), + Error::IO(message) => TypedHttpResponse::return_standard_error(status_code, message.clone()), + Error::Privilege(message) => TypedHttpResponse::return_standard_error(status_code, message.clone()), + Error::Parser(message) => TypedHttpResponse::return_standard_error(status_code, message.clone()), } } }