Fixed macro
This commit is contained in:
parent
6a2aef3436
commit
92beac9893
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -183,7 +183,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "actix-web-utils"
|
||||
version = "0.2.5"
|
||||
version = "0.2.6"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"log",
|
||||
|
@ -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."
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user