Convert DecodingErrors thrown from request handling code to HTTP 400 … #161
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
The library does not gracefully handle when clients send requests with malformed request bodies, parameters, etc. If a client sends a request that for example is missing a required field or has a field that cannot be converted to the correct type, the server should respond with a
400indicating to the client that they have done something wrong. Instead, the library throws aDecodingErrorwhich propagates all the way up resulting in a500response which incorrectly tells the client that something went wrong on the server.Modifications
DecodingErrorsintoRuntimeErrors.ServerErrorconform toHTTPResponseConvertibleErrorHandlingMiddlewareto first check if the underlying error conforms toHTTPResponseConvertibleand if not use the values fromServerError. This allows theHTTPResponseConvertiblevalues set in aRuntimeErrorto be honoured after theRuntimeErroris transformed into aServerError.Result
Since
RuntimeErrorconforms toHTTPResponseConvertiblethese errors will get converted to400responses by theErrorHandlingMiddleware. This isn't a perfect solution because consumers of the library have to opt-in to theErrorHandlingMiddlewareto avoid returning500.Test Plan
400responses.