Retrieving guard error messages #2691
Unanswered
SohamGhugare
asked this question in
Questions
Replies: 1 comment
-
I don't think that catchers have access to failing guard errors. The best way I know to do what you want to do is to catch the error inside your request handler. use rocket::serde::json::Error as JsonError;
// A bit of boilerplate to save characters later
#[derive(Responder)]
#[response(status = 422)]
struct JsonErrorResponse(String);
impl From<JsonError> for JsonErrorResponse {
fn from(err: JsonError) -> Self {
Self(err.to_string())
}
}
// Your original handler, that extracts a Result guard this time, and returns a Result
#[post("/create", format="json", data="<user>")]
pub async fn create_user(user_res: Result<Json<CreateUser>, JsonError>) -> Result<Value, JsonErrorResponse> {
let user = user_res?;
[...]
} (The code is untested, I hope it works!) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
So I have a route which basically takes in a json request and tries to bind it to a struct. I have implemented the binding using the Json data guard, however I'm having issues retrieving the error message passed in by the guard when it fails.
this is my route and when it fails, this is the error thrown,
now I've implemented a catcher for the 422 status errors,
is there a way to pass the error message in the catcher, so instead of plain "unprocessable entity" it should say "unprocessable entity: missing field `name`"
Beta Was this translation helpful? Give feedback.
All reactions