Switched to Err repo

This commit is contained in:
Franklin 2022-09-25 12:08:04 -04:00
parent 6b5fa82486
commit bd7af1029a
9 changed files with 22 additions and 132 deletions

11
Cargo.lock generated
View File

@ -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"

View File

@ -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"] }
log = { version = "0.4", features = ["serde"] }
err = { git = "https://github.com/franklinblanco/err.git" }

View File

@ -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<String>,
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<E: Display>(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) }
}
}

View File

@ -1 +0,0 @@
pub mod message;

View File

@ -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<MessageResource>),
/// 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<Self, Self::Err> {
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<MessageResource> = 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 {}

View File

@ -1,2 +1 @@
pub mod error;
pub mod macro_enums;

View File

@ -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.
/// ```
///

View File

@ -1,5 +1,4 @@
pub mod extensions;
pub mod dtos;
pub mod utils;
pub mod enums;
pub mod traits;

View File

@ -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<T: Serialize>(&self, status_code: u16) -> TypedHttpResponse<T> {
//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()),
}
}
}