Skip to content

Handle non-JSON API responses gracefully#729

Open
Sim-hu wants to merge 1 commit into
Kraigie:masterfrom
Sim-hu:fix-non-json-response-crash
Open

Handle non-JSON API responses gracefully#729
Sim-hu wants to merge 1 commit into
Kraigie:masterfrom
Sim-hu:fix-non-json-response-crash

Conversation

@Sim-hu

@Sim-hu Sim-hu commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Replace Jason.decode! with Jason.decode in API response handling, so that non-JSON responses produce clear errors instead of crashing with a confusing Jason.DecodeError.

Motivation

When Discord is down or a proxy returns an HTML error page (e.g. 502 Bad Gateway), the ratelimiter may forward a non-JSON body that causes Jason.decode! to crash with a misleading error like:

** (Jason.DecodeError) unexpected byte at position 0: 0x3C ("<")

This makes it look like a protocol-level bug when the actual issue is just that Discord is down. As discussed in #417, using Jason.decode (non-bang) and handling the error case provides a much clearer signal.

Changes

  • lib/nostrum/api/helpers.exhandle_request_with_decode/1 and handle_request_with_decode/2 now use Jason.decode and return {:error, %ApiError{}} on decode failure
  • lib/nostrum/util.exget_new_gateway_url/0 now uses Jason.decode and raises a descriptive message on failure

Testing

  • mix compile --warnings-as-errors passes
  • mix test — 212 tests, 0 failures

Closes #417

Replace Jason.decode! with Jason.decode in API response handling
so that non-JSON responses (e.g. HTML error pages from Cloudflare
when Discord is down) produce clear errors instead of confusing
Jason.DecodeError crashes.

Closes Kraigie#417
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.

Blows up in confusing way if Discord is down

1 participant