I suppose one possible solution would be to add a match branch to separate the deserialization errors, something like this:
.map(|res: Result<T, _>| match res {
Ok(data) => data.validate().map(|_| Json(data)).map_err(Error::from),
Err(actix_web::error::JsonPayloadError::Deserialize(e)) => Err(Error::from(e)), // will use From<serde_json::error::Error for Error
Err(e) => Err(Error::from(e)), // will use From<actix_web::error::JsonPayloadError> for Error (like before)
})
Please note that this is hypothetical, I haven't tested the above code at all. 🙂
FWIW, any deserialization error that occurs for Path or Query does result in the correct Error::Deserialize(_) variant being used.
In my code, I created a function to allow me to write a custom error handler for JSON data validation errors. It includes something like this:
I was expected that any error occurring during JSON deserialization (missing field, etc.) would result in an
Error::Deserialize(DeserializeErrors::DeserializeJson(_))
. However, this is not the case - instead, I get anError::JsonPayloadError(JsonPayloadError::Deserialize(_))
.I believe this is caused by this code:
https://github.com/rambler-digital-solutions/actix-web-validator/blob/b8ffc17283e6db518669849005f45a57d8f5197b/src/json.rs#L131-L134
In this
match
'sErr(e)
branch,e
is anactix_web::error::JsonPayloadError
. Thus,Error::from
will use theFrom
implementation forJsonPayloadError
, resulting in anError::JsonPayloadError
.I suppose one possible solution would be to add a
match
branch to separate the deserialization errors, something like this:Please note that this is hypothetical, I haven't tested the above code at all. 🙂
FWIW, any deserialization error that occurs for
Path
orQuery
does result in the correctError::Deserialize(_)
variant being used.