| 44 | // impl ResponseError trait allows to convert our errors into http responses with appropriate data |
| 45 | impl ResponseError for ServiceError { |
| 46 | fn error_response(&self) -> HttpResponse { |
| 47 | match self { |
| 48 | ServiceError::InternalServerError(ref message) => HttpResponse::InternalServerError() |
| 49 | .json(ErrorResponseBody { |
| 50 | message: message.to_string(), |
| 51 | }), |
| 52 | ServiceError::BadRequest(ref message) => { |
| 53 | HttpResponse::BadRequest().json(ErrorResponseBody { |
| 54 | message: message.to_string(), |
| 55 | }) |
| 56 | } |
| 57 | ServiceError::DuplicateTrackingId(ref id) => { |
| 58 | HttpResponse::BadRequest().json(ErrorResponseBody { |
| 59 | message: format!("Stoped overwriting data, Duplicate Tracking Id {:?}", id), |
| 60 | }) |
| 61 | } |
| 62 | ServiceError::Unauthorized => HttpResponse::Unauthorized().json(ErrorResponseBody { |
| 63 | message: "Unauthorized".to_string(), |
| 64 | }), |
| 65 | ServiceError::Forbidden => HttpResponse::Forbidden().json(ErrorResponseBody { |
| 66 | message: "Forbidden".to_string(), |
| 67 | }), |
| 68 | ServiceError::NotFound(ref message) => { |
| 69 | HttpResponse::NotFound().json(ErrorResponseBody { |
| 70 | message: format!("Not Found: {}", message), |
| 71 | }) |
| 72 | } |
| 73 | ServiceError::JsonDeserializeError(ref message) => { |
| 74 | HttpResponse::BadRequest().json(ErrorResponseBody { |
| 75 | message: format!("Json Deserialization Error: {}", message), |
| 76 | }) |
| 77 | } |
| 78 | ServiceError::PayloadTooLarge(ref message) => { |
| 79 | HttpResponse::PayloadTooLarge().json(ErrorResponseBody { |
| 80 | message: message.to_string(), |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // we can return early in our handlers if UUID provided by the user is not valid |