Add error catchers
This commit is contained in:
parent
1559936d4b
commit
5e7e4c658e
|
@ -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",
|
||||||
|
})
|
||||||
|
}
|
|
@ -4,9 +4,10 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate diesel;
|
extern crate diesel;
|
||||||
|
|
||||||
use rocket::{routes, Rocket};
|
use rocket::{catchers, routes, Rocket};
|
||||||
use rocket_contrib::serve::StaticFiles;
|
use rocket_contrib::serve::StaticFiles;
|
||||||
|
|
||||||
|
pub mod catchers;
|
||||||
pub mod handlers;
|
pub mod handlers;
|
||||||
|
|
||||||
fn rocket() -> Rocket {
|
fn rocket() -> Rocket {
|
||||||
|
@ -52,6 +53,10 @@ fn rocket() -> Rocket {
|
||||||
handlers::timeline::destroy,
|
handlers::timeline::destroy,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
.register(catchers![
|
||||||
|
catchers::not_found,
|
||||||
|
catchers::unprocessable_entity
|
||||||
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
Loading…
Reference in New Issue