Skip to content

Comments

Fix unhandled JSON parse error on non-JSON HTTP responses#18

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/add-graceful-failure-response
Draft

Fix unhandled JSON parse error on non-JSON HTTP responses#18
Copilot wants to merge 2 commits intomainfrom
copilot/add-graceful-failure-response

Conversation

Copy link
Contributor

Copilot AI commented Feb 20, 2026

The makeRequest function throws unhandled exceptions when API gateways return HTML error pages (502, 504, etc.) instead of JSON, causing server crashes.

Changes

  • Enhanced error handling in paystack-client.ts:

    • Capture HTTP status code from response object
    • Truncate long responses to 200 chars in error message, preserve full text in error object
    • Replace generic "Invalid JSON response" with context-rich error message
  • Added test coverage in test/paystack-client.spec.ts:

    • Non-JSON responses (HTML error pages)
    • Response truncation behavior
    • Valid JSON parsing regression test

Example

Before:

Error: Invalid JSON response: <html><body><h1>502 Bad Gateway</h1>...

After:

Error: Received non-JSON response from server (HTTP 502): <html><body><h1>502 Bad Gateway</h1>...

The error object now includes statusCode and responseText properties for debugging.

cc @tolu-paystack

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: makeRequest throws unhandled JSON.parse syntax error on non-JSON HTTP responses</issue_title>
<issue_description>### What happened?

The code fails to handle standard HTTP edge cases. If an API gateway returns an HTML error page (like a 502 Bad Gateway), the server crashes due to an unhandled exception.

Version

1.0.2 (Default)

Relevant log output

Code of Conduct

  • I agree to follow the project's Code of Conduct</issue_description>

<agent_instructions>add a graceful failure response when the server returns a non JSON response. Tag me in the p/r for review.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@tolu-paystack Thanks for reporting this @OwenMaine! We'll take a look and share feedback here

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

- Enhanced error message to include HTTP status code
- Truncate long responses to 200 characters for readability
- Attach statusCode and full responseText to error object for debugging
- Added comprehensive test suite for non-JSON response handling

Co-authored-by: Andrew-Paystack <78197464+Andrew-Paystack@users.noreply.github.com>
Copilot AI changed the title [WIP] Add graceful failure response for non-JSON HTTP responses Fix unhandled JSON parse error on non-JSON HTTP responses Feb 20, 2026
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.

[Bug]: makeRequest throws unhandled JSON.parse syntax error on non-JSON HTTP responses

2 participants