-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use crate::{guards::UserErrorMessage}; | ||
use rocket::{serde::json::{json, Value}, Request, catch}; | ||
use crate::guards::JsonValidationError; | ||
|
||
#[catch(403)] | ||
pub fn not_authorized() -> Value { | ||
json!([{"label": "unauthorized", "message": "Not authorized to make request"}]) | ||
} | ||
|
||
#[catch(404)] | ||
pub fn not_found() -> Value { | ||
json!([]) | ||
} | ||
|
||
#[catch(422)] | ||
pub fn unprocessable_entry(req: &Request) -> Value { | ||
json! [{"label": "failed.request", "message": "failed to service request"}] | ||
} | ||
|
||
#[catch(500)] | ||
pub fn internal_server_error(req: &Request) -> Value { | ||
let error_message = req | ||
.local_cache(|| Some(UserErrorMessage("Internal server error".to_owned()))); | ||
|
||
json! [{"label": "internal.error", "message": error_message}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use rocket::{ | ||
data::{self, Data, FromData, Limits}, | ||
http::Status, | ||
request::{self, local_cache, FromRequest, Request}, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
use uuid::Uuid; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
pub struct RequestId(pub Option<Uuid>); | ||
|
||
#[derive(Debug)] | ||
pub struct JsonValidation<T>(pub T); | ||
|
||
#[derive(Debug)] | ||
pub enum JsonValidationError { | ||
ParseError(serde_json::Error), | ||
ReadError | ||
} | ||
|
||
#[derive(Serialize, Debug)] | ||
pub struct UserErrorMessage(pub String); | ||
|
||
/// A Json Data Guard that runs valiation on the deserialized types via | ||
/// the valiation crate. The validation crate requires the derserialized | ||
/// type have the `Validate` trait. | ||
#[rocket::async_trait] | ||
impl<'r, T> FromData<'r> for JsonValidation<T> | ||
where | ||
T: Deserialize<'r>, | ||
{ | ||
type Error = JsonValidationError; | ||
|
||
async fn from_data( | ||
req: &'r Request<'_>, | ||
data: Data<'r>, | ||
) -> data::Outcome<'r, Self> { | ||
match data.open(Limits::JSON).into_string().await { | ||
Ok(value) => { | ||
let string = local_cache!(req, value.into_inner()); | ||
|
||
match serde_json::from_str::<T>(string) | ||
.map_err(|e| JsonValidationError::ParseError(e)) | ||
{ | ||
Ok(e) => | ||
data::Outcome::Success(JsonValidation(e)), | ||
Err(e) => | ||
data::Outcome::Failure((Status::InternalServerError, e)) | ||
} | ||
} | ||
Err(_) => { | ||
data::Outcome::Failure( | ||
(Status::InternalServerError, JsonValidationError::ReadError) | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters