Skip to content

fix(postgrest): return structured error for non-JSON body on 2xx responses - #1474

Merged
spydon merged 1 commit into
mainfrom
lukasklingsbo/sdk-1029-paritypostgrest-return-structured-error-for-non-json-body-on
Jun 26, 2026
Merged

spydon merged 1 commit into
mainfrom
lukasklingsbo/sdk-1029-paritypostgrest-return-structured-error-for-non-json-body-on

Conversation

@spydon

@spydon spydon commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

What

When a PostgREST request succeeds (2xx status) but the response body is not valid JSON (for example, an HTML 502 Bad Gateway page from a proxy, or a truncated response), the SDK now throws a structured PostgrestException carrying the raw body, status code, and reason phrase.

Why

The previous handling caught the FormatException and set body = null, which behaved badly:

  • For .select() (return type PostgrestList), the null flowed into PostgrestList.from(null) and crashed with an opaque TypeError.
  • For .maybeSingle()/.single(), it silently returned null, making a proxy's HTML error page indistinguishable from "no rows".

This is the Dart equivalent of the supabase-js structured-error / throwOnError behavior, since the Dart builder always throws on error rather than returning a {data, error} shape.

Not a breaking change

CSV and pgrst.plan+text bodies are handled before the JSON decode, so the only bodies reaching this branch are genuinely malformed. Today those either crash with an opaque error or return a misleading null, so converting them into a clean PostgrestException is a bug fix.

Tests

Added a non-json-succ mock route returning 200 with an HTML body, covering both .select() and .maybeSingle() paths.

Closes SDK-1029. Parity with supabase/supabase-js#2398.

…onses

A 2xx status does not guarantee a JSON body. A proxy or gateway can return
an HTML error page or a truncated response with a success status. Previously
this either crashed with an opaque type error (on select) or silently
returned null (on maybeSingle/single). Throw a structured PostgrestException
carrying the raw body instead.
@spydon
spydon requested a review from a team as a code owner June 26, 2026 09:18
@github-actions github-actions Bot added the postgrest This issue or pull request is related to postgrest label Jun 26, 2026
@spydon
spydon merged commit 2d7c9cf into main Jun 26, 2026
28 checks passed
@spydon
spydon deleted the lukasklingsbo/sdk-1029-paritypostgrest-return-structured-error-for-non-json-body-on branch June 26, 2026 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

postgrest This issue or pull request is related to postgrest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants