Fixed macro

This commit is contained in:
Franklin 2022-08-31 13:41:59 -04:00
parent 6a2aef3436
commit 92beac9893
4 changed files with 10 additions and 10 deletions

2
Cargo.lock generated
View File

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

View File

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

View File

@ -74,7 +74,7 @@ When you could be doing this:
fn service_layer_function() -> TypedHttpResponse<T> { //T can be whatever you want
// ... Some Business logic
let value_returned_from_match = unwrap_or_return_handled_error!(
function_that_returns_a_result()
function_that_returns_a_result(), T
);
// ... More Business logic
}

View File

@ -1,5 +1,5 @@
/// This is to minimize the amount of matches made in the code
/// Give it a Result<Whatever_you_want_to_return, Error> and it'll
/// Give it a Result<Whatever_you_want_to_return, Error> and the type of the success and it'll
/// Basically unwrap the result if its there and if it isn't it'll return a handled error inside a TypedHttpResponse.
/// Default status code is InternalServerError, if you want something different pass it as the first argument as a u16.
/// If you want to also return the success result, then pass a valid status code u16 as a second argument
@ -7,22 +7,22 @@
#[allow(unused_macros)]
#[macro_export]
macro_rules! unwrap_or_return_handled_error {
( $e:expr ) => {
( $e:expr, $type_of_resp:ty ) => {
match $e {
Ok(value) => value,
Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable(&error, 500)
Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable::<$type_of_resp>::(&error, 500)
}
};
( $error_status_code:literal, $e:expr ) => {
( $error_status_code:literal, $e:expr, $type_of_resp:ty ) => {
match $e {
Ok(value) => value,
Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable(&error, error_status_code)
Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable::<$type_of_resp>::(&error, error_status_code)
}
};
( $error_status_code:literal, $success_status_code:literal, $e:expr) => {
( $error_status_code:literal, $success_status_code:literal, $e:expr, $type_of_resp:ty) => {
match $e {
Ok(value) => return actix_web_utils::typed_response::TypedHttpResponse::return_standard_response($success_status_code, value),
Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable(&error, error_status_code)
Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable::<$type_of_resp>::(&error, error_status_code)
}
}
}