I want my Rocket API to have a route like this:
#[post("create/thing", format = "application/json", data="<thing>")]
When the client sends { "name": "mything" }
, everything should be alright and I know how to do that, but when it sends { "name": "foo" }
it should respond with something like this:
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/json
{
"errors": [
{
"status": "422",
"title": "Invalid thing name",
"detail": "The name for a thing must be at least 4 characters long."
}
]
}
How do I respond with a result like a JSON object and a HTTP status code different than 200 in Rocket?
This is what I tried so far:
impl
FromRequest
for myThing
type. This lets me choose a status code as I can write my ownfrom_request
function, but I can't return anything else.- Registering an error catcher like in this example, but this way I only can react to one HTTP status code without context. I have too many failure modes to reserve one HTTP status code for each.