Skip to content

fix(postgrest): report JSON scalar error bodies as errors - #2880

Open
aniketatgithub wants to merge 1 commit into
supabase:masterfrom
aniketatgithub:fix-2876-scalar-error-body
Open

aniketatgithub wants to merge 1 commit into
supabase:masterfrom
aniketatgithub:fix-2876-scalar-error-body

Conversation

@aniketatgithub

Copy link
Copy Markdown

🔍 Description

What changed?

PostgrestBuilder.processResponse used JSON.parse(body) as the error for a failed reply without checking that the result is an object. A JSON scalar body therefore produced no usable error: a 500 with body null reported success: true, and false, 0 or "" gave a falsy error that throwOnError() never threw. A parsed body that is not an object is now reported as { message: body }, the same shape already used for a body that is not JSON.

Why was this change needed?

PostgREST itself sends an error object, but a proxy, gateway, or function in front of it can send a scalar body. The client must not report such a reply as a success.

Closes #2876

📸 Screenshots/Examples

No UI changes.

🔄 Breaking changes

  • This PR contains no breaking changes

📋 Checklist

  • I have read the Contributing Guidelines
  • My PR title follows the conventional commit format: <type>(<scope>): <description>
  • I have run pnpm nx format to ensure consistent code formatting
  • I have added tests for new functionality (if applicable)
  • I have updated documentation (if applicable)

📝 Additional notes

The new regression tests use a mocked fetch, so they run without a PostgREST server. The full postgrest-js suite needs Docker (PostgREST + PostgreSQL), which is not available on this machine; the mock-based suites pass locally (fetch-errors, openapi-spec, retry, timeout-and-url-length, version-and-constants, headers-serialization).

processResponse used JSON.parse(body) as the error for a failed reply
without checking that the result is an object, so a 500 with body null
reported success: true and false, 0 or empty string gave a falsy error
that throwOnError never threw. A parsed body that is not an object is
now reported as { message: body }, the shape already used for a
non-JSON body.

Signed-off-by: Aniket Kumar Tikariha <aniketkomailkro@gmail.com>
@aniketatgithub
aniketatgithub requested review from a team as code owners November 19, 2026 09:44
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Failed requests with JSON bodies containing null, false, 0, or an empty string now remain correctly identified as errors.
    • Error details preserve the original response body, making failures easier to understand.
    • When configured to throw on errors, requests with a null response body now reject with the expected error type.

For failed responses, processResponse now replaces parsed non-object or null bodies with an error object whose message contains the original response body. Tests cover null, false, 0, and "", and check that throwOnError() rejects for a null body.

Assessment against linked issues

Objective Addressed Explanation
Ensure non-2xx responses with JSON scalar bodies return success: false and an error object; ensure throwOnError() throws [#2876] ✅

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Minimal · up to 3f862

Failed responses with scalar JSON bodies such as null, false, 0, or an empty string now return an error object, and throwOnError() throws as expected. The change is small and covered by mocked tests. No merge-blocking risk was found.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8bcd8a26-8814-4c8b-98d5-c6f3599ef7bd
Commits

Reviewing files that changed from the base of the PR and between 35ae921 and 3f862f7.

Files selected for processing: 2

  • packages/core/postgrest-js/src/PostgrestBuilder.ts
  • packages/core/postgrest-js/test/fetch-errors.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@mark1russell7

Copy link
Copy Markdown

I ran the repro from #2876 against this branch (packages/core/postgrest-js/src at 3f862f7, a fake fetch, no server).

master (840843d):

500 with body null  -> success true, error null, status 500; throwOnError() threw: false
500 with body false -> success false, error false, status 500; throwOnError() threw: false
500 with body 0     -> success false, error 0, status 500; throwOnError() threw: false
500 with body ""    -> success false, error "", status 500; throwOnError() threw: false

This branch (3f862f7):

500 with body null  -> success false, error {"message":"null"}, status 500; throwOnError() threw: true
500 with body false -> success false, error {"message":"false"}, status 500; throwOnError() threw: true
500 with body 0     -> success false, error {"message":"0"}, status 500; throwOnError() threw: true
500 with body ""    -> success false, error {"message":"\"\""}, status 500; throwOnError() threw: true

All four bodies from the issue now give success: false, an error object and a throw from throwOnError(). Thanks for the fix.

An AI coding assistant (Claude Code) ran this check and wrote this text.

This branch has not been deployed

No deployments
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.

postgrest-js: a failed reply whose body is a JSON scalar (null, false, 0 or "") gives no error, and a 500 with the body null gives success: true

2 participants