Skip to content

Throw a 400 Bad Request error instead of 500 on decoding failure - #11

Merged
benrosen78 merged 4 commits into
mainfrom
decoding-error-handling
Aug 27, 2026
Merged

benrosen78 merged 4 commits into
mainfrom
decoding-error-handling

Conversation

@benrosen78

@benrosen78 benrosen78 commented Aug 27, 2026 •

Copy link
Copy Markdown
Member

Currently a bad POST body sent into a LambdaKit Router will return a 500 Internal Server Error. After this PR it will return a 400 Bad Request containing the error message from the JSONDecoder. This PR also writes some simple tests for the Router to document this behavior. I verified that the test fails without the new error throw and passes with the code included. To make tests and other basic code more simple to write, more overloads have been introduced to allow making a POST with a body without passing middleware through. I also split the errors up into their own files in their own directory

public var message: String {
switch self {
case let .decodingFailed(error):
"Invalid request body: \(error)"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should send back an actual decoding error here. While it might be helpful in debug mode something generic like
{ "error": "invalid_request", "message": "The request body could not be decoded." } would be more suited for a prod response.

@benrosen78
benrosen78 merged commit 1f64c27 into main Aug 27, 2026
1 check passed
@benrosen78
benrosen78 deleted the decoding-error-handling branch August 27, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants