Closed allanHaroldMarques closed 1 year ago
If you need to get the error message inside your route handle you could simply use Result<Validated<Json<CatOwnerCreate>>>
which is explained as an example inside the rocket docs
Else if you would like to catch all errors, regardless of the route, you could use the same implementation as the validation_catcher
function of this crate, the CachedValidationErrors
type is already public.
I was able to do, like this:
#[catch(400)]
pub async fn bad_request(req: &Request<'_>) -> ApiError {
let validation_errors = req.local_cache(|| CachedValidationErrors(None)).0.as_ref();
let mut message = "Something goes wrong!".to_string();
if validation_errors.is_some() {
message.clear();
let erros = validation_errors.unwrap().field_errors();
for (_,val) in erros.iter() {
for (error) in val.iter() {
message.push_str(error.message.as_ref().unwrap());
}
}
}
ApiError { code: StatusCode::BadRequest, message: message }
}
Thank you!
I implemented the validation as follows:
In the route:
works well. In the stdout(log) it says:
I have a catch(400) that catches the validator error like this::
Is there any way for me to get the error message in this cacth through the (req) Request ?