Skip to content

Conversation

@Aggarwalmansi
Copy link

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 OK response with an empty body and no logs.

This PR ensures JSON encoding is attempted before writing headers and
properly handles encoding failures.

What changed

  • Encode response payload into a buffer before writing headers
  • Handle JSON encoding errors explicitly
  • Return 500 Internal Server Error when encoding fails
  • Prevent silent response failures

Why this is needed

Go’s JSON encoder does not support values like NaN or Inf.
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

  • Manually verified using a handler returning math.NaN()
  • Confirmed response returns 500 Internal Server Error instead of 200
  • Ran go test ./pkg/gofr/http/...

Related Issue

Fixes #2739

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.

HTTP Responder silently fails in case of math.NaN() is returned

1 participant