Skip to content
This repository was archived by the owner on Jun 9, 2022. It is now read-only.

Commit 153630d

Browse files
committed
Modify fn problem::unpack
- Make problem::unpack infallible.
1 parent b9002a8 commit 153630d

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

src/helpers/problem.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::auth;
22
use http_api_problem::HttpApiProblem as Problem;
3+
use std::convert::Infallible;
34
use warp::http;
45
use warp::{Rejection, Reply};
56

@@ -8,6 +9,7 @@ pub fn build<E: Into<anyhow::Error>>(err: E) -> Rejection {
89
}
910

1011
pub fn pack(err: anyhow::Error) -> Problem {
12+
tracing::info!("pack: {:#?}", err);
1113
let err = match err.downcast::<Problem>() {
1214
Ok(problem) => return problem,
1315

@@ -32,7 +34,7 @@ pub fn pack(err: anyhow::Error) -> Problem {
3234
return Problem::new("Invalid JWT token.")
3335
.set_status(http::StatusCode::BAD_REQUEST)
3436
.set_detail(format!("The passed JWT token were invalid. {}", e))
35-
},
37+
}
3638
_ => (),
3739
}
3840
}
@@ -41,22 +43,38 @@ pub fn pack(err: anyhow::Error) -> Problem {
4143
Problem::with_title_and_type_from_status(http::StatusCode::INTERNAL_SERVER_ERROR)
4244
}
4345

44-
pub async fn unpack(rejection: Rejection) -> Result<impl Reply, Rejection> {
45-
if let Some(problem) = rejection.find::<Problem>() {
46-
let code = problem
47-
.status
48-
.unwrap_or(http::StatusCode::INTERNAL_SERVER_ERROR);
49-
50-
let reply = warp::reply::json(problem);
51-
let reply = warp::reply::with_status(reply, code);
52-
let reply = warp::reply::with_header(
53-
reply,
54-
http::header::CONTENT_TYPE,
55-
http_api_problem::PROBLEM_JSON_MEDIA_TYPE,
56-
);
57-
58-
Ok(reply)
46+
fn reply_from_problem(problem: &Problem) -> impl Reply {
47+
let code = problem
48+
.status
49+
.unwrap_or(http::StatusCode::INTERNAL_SERVER_ERROR);
50+
51+
let reply = warp::reply::json(problem);
52+
let reply = warp::reply::with_status(reply, code);
53+
let reply = warp::reply::with_header(
54+
reply,
55+
http::header::CONTENT_TYPE,
56+
http_api_problem::PROBLEM_JSON_MEDIA_TYPE,
57+
);
58+
59+
reply
60+
}
61+
62+
pub async fn unpack(rejection: Rejection) -> Result<impl Reply, Infallible> {
63+
let reply = if rejection.is_not_found() {
64+
let problem = Problem::with_title_and_type_from_status(http::StatusCode::NOT_FOUND);
65+
reply_from_problem(&problem)
66+
} else if let Some(problem) = rejection.find::<Problem>() {
67+
reply_from_problem(problem)
68+
} else if let Some(e) = rejection.find::<warp::filters::body::BodyDeserializeError>() {
69+
let problem = Problem::new("Invalid Request Body.")
70+
.set_status(http::StatusCode::BAD_REQUEST)
71+
.set_detail(format!("Request body is invalid. {}", e));
72+
reply_from_problem(&problem)
5973
} else {
60-
Err(rejection)
61-
}
74+
let problem =
75+
Problem::with_title_and_type_from_status(http::StatusCode::INTERNAL_SERVER_ERROR);
76+
reply_from_problem(&problem)
77+
};
78+
79+
Ok(reply)
6280
}

0 commit comments

Comments
 (0)