Add error catchers

This commit is contained in:
Paul van Tilburg 2019-06-09 18:16:55 +02:00
parent 1559936d4b
commit 5e7e4c658e
2 changed files with 29 additions and 1 deletions

23
src/catchers.rs Normal file
View File

@ -0,0 +1,23 @@
//! The application error catchers
use rocket::catch;
use rocket_contrib::json;
use rocket_contrib::json::JsonValue;
/// Catches an HTTP 404 (Not Found) error.
#[catch(404)]
pub fn not_found() -> JsonValue {
json!({
"status": "error",
"reason": "Resource was not found",
})
}
/// Catches an HTTP 422 (Unprocessable Entity) error.
#[catch(422)]
pub fn unprocessable_entity() -> JsonValue {
json!({
"status": "error",
"reason": "Could not parse JSON body or fields were missing",
})
}

View File

@ -4,9 +4,10 @@
#[macro_use]
extern crate diesel;
use rocket::{routes, Rocket};
use rocket::{catchers, routes, Rocket};
use rocket_contrib::serve::StaticFiles;
pub mod catchers;
pub mod handlers;
fn rocket() -> Rocket {
@ -52,6 +53,10 @@ fn rocket() -> Rocket {
handlers::timeline::destroy,
],
)
.register(catchers![
catchers::not_found,
catchers::unprocessable_entity
])
}
fn main() {