actix_web_utils/utils/
macros.rs

1/// This is to minimize the amount of matches made in the code
2/// Give it a Result<Whatever_you_want_to_return, Error> and the type of the success and it'll
3/// Basically unwrap the result if its there and if it isn't it'll return a handled error inside a TypedHttpResponse.
4/// Default status code is InternalServerError, if you want something different pass it as the first argument as a u16.
5/// If you want to also return the success result, then pass a valid status code u16 as a second argument
6/// Sorry for defining the error status code first, it's to maintain uniform order. 
7#[allow(unused_macros)]
8#[macro_export]
9macro_rules! unwrap_or_return_handled_error {
10    ( $e:expr, $type_of_resp:ty ) => {
11        match $e {
12            Ok(value) => value,
13            Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable::<$type_of_resp>(&error, 500)
14        }
15    };
16    ( $error_status_code:literal, $e:expr, $type_of_resp:ty ) => {
17        match $e {
18            Ok(value) => value,
19            Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable::<$type_of_resp>(&error, $error_status_code)
20        }
21    };
22    ( $error_status_code:literal, $success_status_code:literal, $e:expr, $type_of_resp:ty) => {
23        match $e {
24            Ok(value) => return actix_web_utils::extensions::typed_response::TypedHttpResponse::return_standard_response($success_status_code, value),
25            Err(error) => return actix_web_utils::traits::macro_traits::ReturnableErrorShape::convert_to_returnable::<$type_of_resp>(&error, $error_status_code)
26        }
27    }
28}
29
30/// Takes whatever error you supply to it and wraps it in a GenericError<E> if err
31#[allow(unused_macros)]
32#[macro_export]
33macro_rules! wrap_generic_error_in_wrapper {
34    ( $e:expr ) => {
35        match $e {
36            Ok(value) => Ok(value),
37            Err(error) => Err(actix_web_utils::extensions::generic_error::GenericError::wrap(error)),
38        }
39    }
40}