aboutsummaryrefslogtreecommitdiffstats
path: root/src/error.rs
diff options
context:
space:
mode:
authorYigit Sever2021-04-13 20:37:54 +0300
committerYigit Sever2021-04-13 20:37:54 +0300
commit23e4f52284ad5fa0b068220c54a255ff9fa7b18f (patch)
tree627671c12ea45f8a516601141e52005803384d47 /src/error.rs
parentb16da2c2b64f753129bb33b734a7982e2622ddd3 (diff)
downloadgradecoin-23e4f52284ad5fa0b068220c54a255ff9fa7b18f.tar.gz
gradecoin-23e4f52284ad5fa0b068220c54a255ff9fa7b18f.tar.bz2
gradecoin-23e4f52284ad5fa0b068220c54a255ff9fa7b18f.zip
Add verbose error messages
Not happy with the solution one bit but using error.rs at https://blog.logrocket.com/create-an-async-crud-web-service-in-rust-with-warp/ was not working Basically we just handcraft every single response on the spot, there is some repetition and it's ugly but need to move on
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..7339a06
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,38 @@
1use log::warn;
2use serde::Serialize;
3use std::convert::Infallible;
4use warp::{http::StatusCode, Rejection, Reply};
5
6#[derive(Serialize)]
7struct ErrorResponse {
8 message: String,
9}
10
11pub async fn handle_rejection(err: Rejection) -> std::result::Result<impl Reply, Infallible> {
12 let code;
13 let message;
14
15 if err.is_not_found() {
16 code = StatusCode::NOT_FOUND;
17 message = "Requested resource is not found";
18 } else if let Some(_) = err.find::<warp::filters::body::BodyDeserializeError>() {
19 code = StatusCode::BAD_REQUEST;
20 message = "Error: JSON body is not formatted correctly, check your payload";
21 } else if let Some(_) = err.find::<warp::reject::MissingHeader>() {
22 code = StatusCode::METHOD_NOT_ALLOWED;
23 message = "Error: Authorization header missing, cannot authorize";
24 } else if let Some(_) = err.find::<warp::reject::MethodNotAllowed>() {
25 code = StatusCode::METHOD_NOT_ALLOWED;
26 message = "Error: method not allowed on this endpoint";
27 } else {
28 warn!("unhandled error: {:?}", err);
29 code = StatusCode::INTERNAL_SERVER_ERROR;
30 message = "Internal Server Error";
31 }
32
33 let json = warp::reply::json(&ErrorResponse {
34 message: message.to_owned(),
35 });
36
37 Ok(warp::reply::with_status(json, code))
38}