fromstr or unexpectedstatus code and unspecified error

This commit is contained in:
Franklin 2022-09-20 00:24:55 -04:00
parent e05fe32f4c
commit 6cde64a87a
3 changed files with 38 additions and 3 deletions

2
Cargo.lock generated
View File

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

View File

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

View File

@ -1,4 +1,4 @@
use std::fmt::{self};
use std::{fmt::{self}, str::FromStr};
use crate::dtos::message::MessageResource;
@ -36,4 +36,39 @@ impl fmt::Display for Error{
}
}
}
impl FromStr for Error {
type Err = Error;
fn from_str(string: &str) -> Result<Self, Self::Err> {
let error_name_option = string.get(14..24);
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 Err(Self::Unspecified)
} else if error_name_whole.starts_with("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 Err(Self::UnexpectedStatusCode(expected_status_code.parse().unwrap(), actual_status_code.parse().unwrap(), message_resources));
} else if error_name_whole.starts_with("Client") {
} else if error_name_whole.starts_with("Network") {
} else if error_name_whole.starts_with("Serialization") {
} else if error_name_whole.starts_with("Database") {
} else if error_name_whole.starts_with("Compute") {
}
Ok(Error::Unspecified)
}
}
impl std::error::Error for Error {}