purescript-payload
purescript-payload copied to clipboard
How to return 400 with a message on body decoding error?
I want to return Bad Request
response with my body parsing errors (returned by decodeBody
) when a request contains invalid body. Currently I'm getting 404
with an empty body.
Do you think that it is a good idea? Is it possible to do?
I think returning 400 Bad Request on failures decoding bodies is a better default, so I just published an update to make it so. But now I'm realizing I may have missed the point here so I'm re-opening. To return your own errors from body parsing errors, you "should" be able to either handle the error in your endpoint with body :: Maybe a
or body :: Either String a
, or possibly implement decodeBody
and return the error response from there. It looks like neither of these is supported at the moment, though, so that might be the next item for the backlog.
Hi,
Thanks for the quick feedback.
(..) possibly implement
decodeBody
and return the error response from there.
Yeah, I'm doing exactly that - I'm returning an Left errorMsg
from my decodeBody
.
It looks like neither of these is supported at the moment, though, so that might be the next item for the backlog.
I wonder how we could approach this. It seems that Outcome
drives the flow of handlers chain execution. Do we want to extend Failure
with a message or even allow it to carry full response and change the type of the result of decodeBody
method to something like Either Outcome body
?
Right now I'm thinking maybe this could be unified with the request guard type and decodeBody
would return Result a
i.e. type Result a = ExceptT Failure Aff a
. It looks like Rust Rocket uses this style https://api.rocket.rs/v0.4/rocket/data/trait.FromData.html. Possibly URL parameter and query parameter decoding should also use this.
Hi, Isn't it just replacing
badRequest _ = Error $ Resp.badRequest EmptyBody
by
badRequest s = Error $ Resp.badRequest (StringBody s)
in Handleable.purs
to see the Left value from DecodeBody
?