MCPcopy Create free account
hub / github.com/actix/examples / error_response

Method error_response

basics/error-handling/src/main.rs:56–84  ·  view source on GitHub ↗
(&self)

Source from the content-addressed store, hash-verified

54/// Actix Web uses `ResponseError` for conversion of errors to a response
55impl ResponseError for CustomError {
56 fn error_response(&self) -> HttpResponse {
57 let (status_code, error_msg) = match self {
58 CustomError::CustomOne => {
59 log::error!("Forbidden error: {}", self);
60 (403, self.to_string())
61 }
62 CustomError::CustomTwo => {
63 log::error!("Unauthorized error: {}", self);
64 (401, self.to_string())
65 }
66 CustomError::CustomThree => {
67 log::error!("Internal server error: {}", self);
68 (500, self.to_string())
69 }
70 CustomError::CustomFour => {
71 log::error!("Bad request error: {}", self);
72 (400, self.to_string())
73 }
74 };
75
76 let error_response = ErrorResponse {
77 code: status_code,
78 message: error_msg,
79 error_type: format!("{:?}", self),
80 };
81
82 HttpResponse::build(actix_web::http::StatusCode::from_u16(status_code).unwrap())
83 .json(error_response)
84 }
85}
86
87/// randomly returns either () or one of the 4 CustomError variants

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected