actix_web_utils/enums/
error.rs

1use std::{fmt::{self}, str::FromStr};
2
3use crate::dtos::message::MessageResource;
4
5
6/// This is supposed to be used whenever you have an error in your code and want to be more specific about it. 
7/// Fits in with most CRUD web apps. What you send back to the client is a MessageResource, not the error itself!
8#[derive(Debug, Clone)]
9pub enum Error{
10    /// Takes a Message and the query
11    DatabaseError(MessageResource, String),
12    /// Same as UnexpectedStatusCode but without the extra details.
13    ClientError(MessageResource),
14    /// 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.
15    UnexpectedStatusCode(u16, u16, Vec<MessageResource>),
16    /// Try and never use this error, unless you really need to.
17    Unspecified,
18    /// If you had an error serializing/deserializing and wish to display more details. Such as the entire Json as a string, this is how.
19    SerdeError(MessageResource, String),
20    /// Normally used in compute heavy operations, such as Hashing.
21    ComputeError(MessageResource),
22    /// Self explanatory, Network related error.
23    NetworkError(MessageResource)
24    
25}
26impl fmt::Display for Error{
27    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28        match *&self {
29            Error::Unspecified => write!(f, "Error of type Unspecified."),
30            Error::NetworkError(message) => write!(f, "Error of type Network.\nMessageResource: {}", message),
31            Error::UnexpectedStatusCode(expected, actual, messages) => write!(f, "Error of type UnexpectedStatusCode.\nExpected: {}\nActual: {}\nreceivedMessageResources: {:?}", expected, actual, messages),
32            Error::ClientError(message) => write!(f, "Error of type Client.\nMessageResource: {}", message),
33            Error::SerdeError(message, recieved) => write!(f, "Error of type Serialization/Deserialization.\nMessageResource: {:?}, Object attempted to be serded: {}", message, recieved),
34            Error::DatabaseError(message, query) => write!(f, "Error of type Database.\nMessageResource: {}, \nQuery: {}", message, query),
35            Error::ComputeError(message) => write!(f, "Error of type Compute.\nMessageResource: {}", message),
36        }
37    }
38}
39impl FromStr for Error {
40    type Err = Error;
41
42    fn from_str(string: &str) -> Result<Self, Self::Err> {
43        let error_name_option = string.get(13..25);
44        let error_name_whole = match error_name_option {
45            Some(error_name_whole) => error_name_whole,
46            None => return Err(Error::Unspecified),
47        };
48        if error_name_whole.starts_with("Unspecified") {
49            return Ok(Self::Unspecified)
50        }
51        if error_name_whole.contains("UnexpectedStatusCode") {
52            let expected_str_index = string.find("Expected: ").unwrap() + 10;
53            let actual_str_index = string.find("Actual: ").unwrap() + 8;
54            let expected_status_code = string.get(expected_str_index..expected_str_index+3).unwrap();
55            let actual_status_code = string.get(actual_str_index..actual_str_index+3).unwrap();
56            
57            let message_resources_string = string.get(string.find("receivedMessageResources").unwrap() + 26..string.len() - 1).unwrap();
58            let message_resources: Vec<MessageResource> = serde_json::from_str(message_resources_string).unwrap();
59            return Ok(Self::UnexpectedStatusCode(expected_status_code.parse().unwrap(), actual_status_code.parse().unwrap(), message_resources));
60        }
61        if error_name_whole.starts_with("Client") {
62
63        }
64        if error_name_whole.starts_with("Network") {
65
66        }
67        if error_name_whole.starts_with("Serialization") {
68
69        }
70        if error_name_whole.starts_with("Database") {
71
72        } 
73        if error_name_whole.starts_with("Compute") {
74
75        }
76
77        Ok(Error::ClientError(MessageResource::new_from_str("msg")))
78    }
79}
80impl std::error::Error for Error {}