2020-06-26 03:08:32 +00:00
|
|
|
use rocket::http::Status;
|
2020-06-26 11:14:07 +00:00
|
|
|
use rocket::response::status;
|
|
|
|
use rocket::Rocket;
|
2020-06-26 03:08:32 +00:00
|
|
|
use rocket_contrib::json::Json;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
2020-07-01 17:55:27 +00:00
|
|
|
#[catch(400)]
|
|
|
|
pub fn bad_request() -> status::Custom<Json<Value>> {
|
|
|
|
status::Custom(
|
|
|
|
Status::BadRequest,
|
|
|
|
Json(json!({
|
|
|
|
"err": "bad request",
|
|
|
|
"msg": "Error parsing JSON body"
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-26 03:08:32 +00:00
|
|
|
#[catch(404)]
|
2020-07-01 17:55:27 +00:00
|
|
|
pub fn not_found() -> status::Custom<Json<Value>> {
|
2020-06-26 11:14:07 +00:00
|
|
|
status::Custom(
|
|
|
|
Status::NotFound,
|
|
|
|
Json(json!({
|
|
|
|
"err":"route not found",
|
|
|
|
"msg": "The given route does not exist"
|
|
|
|
})),
|
|
|
|
)
|
2020-06-26 03:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[catch(422)]
|
2020-07-01 17:55:27 +00:00
|
|
|
pub fn unprocessable_entity() -> status::Custom<Json<Value>> {
|
2020-06-26 11:14:07 +00:00
|
|
|
status::Custom(
|
|
|
|
Status::UnprocessableEntity,
|
|
|
|
Json(json!({
|
|
|
|
"err":"failed to process entity",
|
|
|
|
"msg": "The given object could not be processed. This could be due to sending \
|
|
|
|
malformed or incomplete JSON objects in the request body."
|
|
|
|
})),
|
|
|
|
)
|
2020-06-26 03:08:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-26 12:07:32 +00:00
|
|
|
#[catch(403)]
|
|
|
|
pub fn forbidden() -> status::Custom<Json<Value>> {
|
|
|
|
status::Custom(
|
|
|
|
Status::Forbidden,
|
|
|
|
Json(json!({
|
|
|
|
"err":"forbidden",
|
|
|
|
"msg": "You are not allowed to modify this resource"
|
|
|
|
})),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-26 03:08:32 +00:00
|
|
|
#[catch(500)]
|
2020-07-01 17:55:27 +00:00
|
|
|
pub fn internal_server_error() -> status::Custom<Json<Value>> {
|
2020-06-26 11:14:07 +00:00
|
|
|
status::Custom(
|
2020-12-17 15:56:00 +00:00
|
|
|
Status::InternalServerError,
|
2020-06-26 11:14:07 +00:00
|
|
|
Json(json!({
|
|
|
|
"err":"internal server error",
|
|
|
|
"msg": "Something went wrong, try again"
|
|
|
|
})),
|
|
|
|
)
|
2020-06-26 03:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fuel(rocket: Rocket) -> Rocket {
|
2020-06-26 11:14:07 +00:00
|
|
|
rocket.register(catchers![
|
2020-07-01 17:55:27 +00:00
|
|
|
bad_request,
|
2020-06-26 11:14:07 +00:00
|
|
|
not_found,
|
|
|
|
unprocessable_entity,
|
|
|
|
internal_server_error
|
|
|
|
])
|
|
|
|
}
|