fix(http): handle JSON encoding errors in responder #2766
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.
Description
The HTTP responder was writing headers before attempting JSON encoding.
When the encoder failed (e.g.
math.NaN()), the error was silently ignored,resulting in a
200 OKresponse with an empty body and no logs.This PR ensures JSON encoding is attempted before writing headers and
properly handles encoding failures.
What changed
500 Internal Server Errorwhen encoding failsWhy this is needed
Go’s JSON encoder does not support values like
NaNorInf.Previously, such cases caused the server to respond with a misleading success status.
This change ensures correct HTTP semantics and improves debuggability.
How it was tested
math.NaN()500 Internal Server Errorinstead of200go test ./pkg/gofr/http/...Related Issue
Fixes #2739