Skip to main content

foxtive_ntex/http/response/
message.rs

1use crate::contracts::ResponseCodeContract;
2use crate::enums::ResponseCode;
3use crate::http::responder::Responder;
4use crate::http::response::ext::AppMessageExt;
5use crate::http::{HttpError, HttpResult, IntoHttpResult};
6use foxtive::prelude::AppMessage;
7use foxtive::results::AppResult;
8use ntex::http::error::BlockingError;
9
10impl AppMessageExt for AppMessage {
11    fn respond(self) -> HttpResult {
12        let status = self.status_code();
13        match status.is_success() {
14            true => Ok(Responder::message(
15                &self.message(),
16                ResponseCode::from_status(self.status_code()),
17            )),
18            false => Err(HttpError::AppMessage(self)),
19        }
20    }
21}
22
23impl AppMessageExt for AppResult<AppMessage> {
24    fn respond(self) -> HttpResult {
25        match self {
26            Ok(msg) => msg.respond(),
27            Err(err) => Err(HttpError::AppError(err)),
28        }
29    }
30}
31
32impl AppMessageExt for Result<AppMessage, AppMessage> {
33    fn respond(self) -> HttpResult {
34        match self {
35            Ok(msg) => msg.respond(),
36            Err(err) => err.respond(),
37        }
38    }
39}
40
41impl AppMessageExt for Result<AppMessage, BlockingError<AppMessage>> {
42    fn respond(self) -> HttpResult {
43        match self {
44            Ok(msg) => msg.respond(),
45            Err(err) => match err {
46                BlockingError::Error(msg) => msg.respond(),
47                BlockingError::Canceled => AppMessage::InternalServerError.into_http_result(),
48            },
49        }
50    }
51}
52
53impl AppMessageExt for Result<AppMessage, BlockingError<foxtive::Error>> {
54    fn respond(self) -> HttpResult {
55        match self {
56            Ok(msg) => msg.respond(),
57            Err(err) => match err {
58                BlockingError::Error(err) => Err(HttpError::AppError(err)),
59                BlockingError::Canceled => AppMessage::InternalServerError.into_http_result(),
60            },
61        }
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use crate::http::response::ext::AppMessageExt;
68    use foxtive::Error;
69    use foxtive::prelude::AppMessage;
70    use ntex::http::StatusCode;
71    use ntex::http::error::BlockingError;
72    use ntex::web::WebResponseError;
73
74    #[test]
75    fn test_app_message_respond_success() {
76        let msg = AppMessage::SuccessMessage("Yes");
77        let result = msg.respond();
78        assert!(result.is_ok());
79    }
80
81    #[test]
82    fn test_app_message_respond_error() {
83        let msg = AppMessage::InternalServerError;
84        let result = msg.respond();
85        assert!(result.is_err());
86    }
87
88    #[test]
89    fn test_app_message_result_respond() {
90        let msg: Result<AppMessage, Error> = Ok(AppMessage::InternalServerError);
91        let result = msg.respond();
92        assert!(result.is_err());
93    }
94
95    #[test]
96    fn test_app_message_result_error_respond() {
97        let msg = Err(AppMessage::InternalServerError);
98        let result = msg.respond();
99        assert!(result.is_err());
100    }
101
102    #[test]
103    fn test_app_message_result_blocking_error_respond() {
104        let msg = Err(BlockingError::Error(foxtive::Error::from(
105            AppMessage::InternalServerError,
106        )));
107        let result = msg.respond();
108        assert!(result.is_err());
109    }
110
111    #[test]
112    fn test_app_message_result_blocking_error_canceled_respond() {
113        let msg: Result<AppMessage, BlockingError<AppMessage>> = Err(BlockingError::Canceled);
114        let result = msg.respond();
115        assert!(result.is_err());
116
117        let msg: Result<AppMessage, BlockingError<AppMessage>> =
118            Ok(AppMessage::SuccessMessage("Yep"));
119        let status = msg.respond().unwrap().status();
120        assert_eq!(status, StatusCode::OK);
121
122        let msg: Result<AppMessage, BlockingError<AppMessage>> =
123            Err(BlockingError::Error(AppMessage::WarningMessage("Hmm")));
124        let status = msg.respond().unwrap_err().status_code();
125        assert_eq!(status, StatusCode::BAD_REQUEST);
126    }
127
128    #[test]
129    fn test_app_message_result_blocking_error_canceled_respond_with_error() {
130        let msg: Result<AppMessage, BlockingError<Error>> = Err(BlockingError::Canceled);
131        let result = msg.respond();
132        assert!(result.is_err());
133    }
134}