1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use axum::body::Body;
use axum::response::Response;
use doido_controller::rescue::RescueHandlers;
use http::StatusCode;
#[derive(Debug)]
struct RecordNotFound;
impl std::fmt::Display for RecordNotFound {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "record not found")
}
}
impl std::error::Error for RecordNotFound {}
fn handlers() -> RescueHandlers {
RescueHandlers::new().on::<RecordNotFound>(|_e| {
Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Body::from("Not Found"))
.unwrap()
})
}
#[test]
fn registered_error_is_rescued_to_its_response() {
let err = doido_core::anyhow::Error::new(RecordNotFound);
let resp = handlers()
.rescue(&err)
.expect("registered error is handled");
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
#[test]
fn unregistered_error_is_not_rescued() {
let err = doido_core::anyhow::anyhow!("some other failure");
assert!(
handlers().rescue(&err).is_none(),
"an unregistered error falls through to the default 500"
);
}