Return 422 for semantically bad Json, MessagePack.

This commit is contained in:
Sergio Benitez 2021-04-07 22:54:32 -07:00
parent c45a83897f
commit 1c70df052c
2 changed files with 10 additions and 0 deletions

View File

@ -181,7 +181,11 @@ impl<'r, T: Deserialize<'r>> FromData<'r> for Json<T> {
Err(JsonError::Io(e)) if e.kind() == io::ErrorKind::UnexpectedEof => {
Outcome::Failure((Status::PayloadTooLarge, JsonError::Io(e)))
},
Err(JsonError::Parse(s, e)) if e.classify() == serde_json::error::Category::Data => {
Outcome::Failure((Status::UnprocessableEntity, JsonError::Parse(s, e)))
},
Err(e) => Outcome::Failure((Status::BadRequest, e)),
}
}
}

View File

@ -167,6 +167,12 @@ impl<'r, T: Deserialize<'r>> FromData<'r> for MsgPack<T> {
Err(Error::InvalidDataRead(e)) if e.kind() == io::ErrorKind::UnexpectedEof => {
Outcome::Failure((Status::PayloadTooLarge, Error::InvalidDataRead(e)))
},
| Err(e@Error::TypeMismatch(_))
| Err(e@Error::OutOfRange)
| Err(e@Error::LengthMismatch(_))
=> {
Outcome::Failure((Status::UnprocessableEntity, e))
},
Err(e) => Outcome::Failure((Status::BadRequest, e)),
}
}