Skip to content

Commit 4374356

Browse files
committed
feat: add catchers
1 parent a4d349c commit 4374356

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/catchers/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,20 @@ pub fn not_found(req: &Request) -> Json<ErrorResponse> {
2323
status: 404,
2424
message: "Page not found".to_string(),
2525
})
26+
}
27+
28+
#[catch(500)]
29+
pub fn internal_server_error(req: &Request) -> Json<ErrorResponse> {
30+
Json(ErrorResponse {
31+
status: 500,
32+
message: "Internal Server Error".to_string(),
33+
})
34+
}
35+
36+
#[catch(204)]
37+
pub fn no_content(req: &Request) -> Json<ErrorResponse> {
38+
Json(ErrorResponse {
39+
status: 204,
40+
message: "No Content".to_string(),
41+
})
2642
}

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fn index() -> &'static str {
2828
fn rocket() -> _ {
2929
dotenv().ok();
3030

31-
rocket::build().mount("/", routes![index, create_snack, list_snacks, update_snack, delete_snack]).register("/", catchers![catchers::unauthorized, catchers::not_found])
31+
rocket::build().mount("/", routes![index, create_snack, list_snacks, update_snack, delete_snack]).register("/", catchers![catchers::unauthorized, catchers::not_found,
32+
catchers::no_content, catchers::internal_server_error])
3233
}
3334

0 commit comments

Comments
 (0)