Repository navigation
refactor!: split every service exception into a client and an api base - #1644
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughThe PR adds ChangesShared exception contract
Authentication exceptions
Functions exceptions
PostgREST exceptions
Storage exceptions
Estimated code review effort: 4 (Complex) | ~45 minutes Suggested labels: Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
examples/edge_functions/lib/main.dart (1)
363-367: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winHandle transport failures without rendering a null status.
When a request fails before a response,
statusCodeis null. The current fallback displaysFunction failed with status null. Useerror.messagefor that case.Proposed fix
- : 'Function failed with status ${error.statusCode}'; + : error.statusCode == null + ? error.message + : 'Function failed with status ${error.statusCode}';🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@examples/edge_functions/lib/main.dart` around lines 363 - 367, Update the FunctionException fallback in the error-handling flow to use error.message when error.statusCode is null, avoiding a rendered “status null” message. Preserve the existing details['error'] handling and continue using the status-based fallback when a status code is available.packages/postgrest/lib/src/types.dart (1)
27-49: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick winPreserve normalized fields in
PostgrestException.fromJson().
toJson()writesstatusCodeanderrorCode, but the constructor only passes those fields in through separate arguments. When an app serializes an example exception, then deserializes the JSON withPostgrestException.fromJson(...),statusCodeanderrorCodedisappear. Add fallback reads forjson['statusCode']andjson['errorCode'], and cover this in a round-trip test.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/postgrest/lib/src/types.dart` around lines 27 - 49, Update PostgrestException.fromJson() to use json['statusCode'] and json['errorCode'] as fallbacks when the statusCode and errorCode arguments are absent, preserving explicitly supplied arguments. Add a round-trip test covering toJson() followed by fromJson() and verifying both normalized fields remain unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/storage_client/lib/src/fetch.dart`:
- Around line 40-50: Update the error-body parsing flow around
StorageException.fromJson so valid non-object JSON values, including arrays,
strings, and null, are normalized to a StorageException containing the raw
error.body instead of triggering an uncaught TypeError; preserve the existing
object parsing and FormatException behavior, and add a regression test covering
a non-object JSON HTTP error response.
In `@packages/supabase_common/lib/src/supabase_exception.dart`:
- Around line 3-5: Correct the exception-contract documentation so it applies
only to exceptions extending SupabaseException, rather than every service
exception. Update the documentation comment in
packages/supabase_common/lib/src/supabase_exception.dart at lines 3-5 and the
inheritance statement in packages/supabase_common/README.md at lines 14-16;
explicitly scope the README statement to the migrated exception types and leave
RealtimeSubscribeException and IcebergException excluded.
---
Outside diff comments:
In `@examples/edge_functions/lib/main.dart`:
- Around line 363-367: Update the FunctionException fallback in the
error-handling flow to use error.message when error.statusCode is null, avoiding
a rendered “status null” message. Preserve the existing details['error']
handling and continue using the status-based fallback when a status code is
available.
In `@packages/postgrest/lib/src/types.dart`:
- Around line 27-49: Update PostgrestException.fromJson() to use
json['statusCode'] and json['errorCode'] as fallbacks when the statusCode and
errorCode arguments are absent, preserving explicitly supplied arguments. Add a
round-trip test covering toJson() followed by fromJson() and verifying both
normalized fields remain unchanged.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 1746a526-08a7-4060-8c83-db9eeecb782a
📒 Files selected for processing (39)
examples/edge_functions/integration_test/functions_test.dartexamples/edge_functions/integration_test/invoke_test.dartexamples/edge_functions/lib/main.dartpackages/functions_client/example/functions_dart_example.dartpackages/functions_client/lib/functions_client.dartpackages/functions_client/lib/src/functions_client.dartpackages/functions_client/lib/src/types.dartpackages/functions_client/test/functions_dart_test.dartpackages/gotrue/lib/gotrue.dartpackages/gotrue/lib/src/fetch.dartpackages/gotrue/lib/src/gotrue_client.dartpackages/gotrue/lib/src/types/auth_exception.dartpackages/gotrue/test/client_test.dartpackages/gotrue/test/fetch_test.dartpackages/gotrue/test/otp_mock_test.dartpackages/gotrue/test/passkey_test.dartpackages/gotrue/test/provider_test.dartpackages/gotrue/test/src/gotrue_oauth_api_test.dartpackages/gotrue/test/src/types/auth_exception_test.dartpackages/gotrue/test/web3_auth_test.dartpackages/postgrest/example/main.dartpackages/postgrest/lib/postgrest.dartpackages/postgrest/lib/src/postgrest_builder.dartpackages/postgrest/lib/src/types.dartpackages/postgrest/test/basic_test.dartpackages/postgrest/test/transforms_test.dartpackages/postgrest/test/upsert_test.dartpackages/storage_client/lib/src/fetch.dartpackages/storage_client/lib/src/storage_file_api.dartpackages/storage_client/lib/src/types.dartpackages/storage_client/lib/storage_client.dartpackages/storage_client/test/basic_test.dartpackages/storage_client/test/client_test.dartpackages/storage_client/test/types_test.dartpackages/supabase_common/README.mdpackages/supabase_common/lib/src/supabase_exception.dartpackages/supabase_common/lib/supabase_common.dartpackages/supabase_common/test/supabase_exception_test.dartpackages/supabase_flutter/test/deep_link_test.dart
d68a53b to
aa160d3
Compare
556df34 to
68959ca
Compare
|
We have cases where an exception is thrown with only a message for some bad state in the client e.g. supabase-flutter/packages/gotrue/lib/src/gotrue_client.dart Lines 1069 to 1079 in bbf9b74 So these cases have no status or error code. Currently they are null in these cases. Error codes and status codes are only present for http/supabase api calls. So I'm wondering if it makes sense to separate those api exceptions from these client only exceptions and maybe even have a class with status code being non-nullable. Such that we have a |
Sounds like a good idea! |
68959ca to
6667939
Compare
|
@Vinzent03 what do you think about this solution? abstract class SupabaseException implements Exception {
final String message;
final String? errorCode;
const SupabaseException(this.message, {this.errorCode});
}
mixin SupabaseApiException on SupabaseException {
int get statusCode;
} |
|
I think I really like the new style. But some small things:
Also may I ask for the reason of excluding the realtimesubscribe exception and iceberg here? Haven't looked into it really, but just wondering. |
Sounds like good suggestions, I'll fix those up!
Because they have zero overlap with the other exception structure. EDIT: EDIT2: |
`AuthException`, `StorageException`, `PostgrestException` and `FunctionException` each reimplemented the same message plus status shape under different field names and types. They now extend a single `SupabaseException` in `supabase_common`, so `on SupabaseException` catches a failure from any service. Reconciling the four shapes means: - `statusCode` is an `int?` everywhere. It was a `String?` in auth and storage, and `FunctionException.status` (an `int`) in functions. - The service specific code is `errorCode` everywhere: `AuthException.code`, `StorageException.error` and `PostgrestException.code` are gone. `PostgrestException.errorCode` now only holds the PostgREST/PostgreSQL code, since the HTTP status has its own field. - `PostgrestException.fromJson` takes `statusCode` instead of `code`. - `FunctionException` carries a `message` like the other exceptions. `reasonPhrase` is gone: the response's reason phrase becomes the message, falling back to a per-subtype default when the response has none. - `FunctionsFetchException.statusCode` is `null` instead of `0`, since no response reached the client. - Auth exception subclasses no longer each repeat `toString`; the base prints the concrete runtime type. Part of #1572 (tier 3), under the v3 umbrella #1278.
The exceptions in this package now carry the HTTP status as `statusCode`, so the successful response reporting it as `status` was the odd one out. It also lines up with `http.Response.statusCode`.
…ypes The base class doc and the README claimed every service exception extends `SupabaseException`, but `RealtimeSubscribeException` and `IcebergException` deliberately keep shapes of their own.
`_handleError` cast the decoded error body to `Map<String, dynamic>` inside a `try`/`on FormatException`. A body that parses as JSON but is not an object, for example the `["upstream connect error"]` a gateway can return, throws a `TypeError` on that cast, which the `on FormatException` does not catch, so it escaped instead of surfacing as a `StorageException`. The non-response branch also moved up front, which drops a level of nesting.
`SupabaseException` reported a `statusCode` and an `errorCode` that were
always null for the failures the client raises itself, such as a missing
session or a redirect url without an access token. The layer a failure came
from was not visible from its type.
`SupabaseException` now carries only the message and the error code, and a
new `SupabaseApiException` mixin adds the response's `statusCode` as a
non-nullable `int`. Catching it handles any failure a service answered with,
whichever service that was:
```dart
try {
await supabase.from('countries').select();
} on SupabaseApiException catch (error) {
print('${error.statusCode}: ${error.message}');
}
```
Per package:
- `AuthException` is message plus error code. `AuthApiException` carries the
status, and `AuthWeakPasswordException` now extends it. Both
`AuthRetryableFetchException` and `AuthUnknownException` straddle the two
layers, so they keep a nullable `statusCode` of their own.
- `AuthSessionMissingException` and `AuthInvalidJwtException` no longer
report a fabricated `400`; they report `session_missing` and `invalid_jwt`.
- `getSessionFromUrl` throws an `AuthApiException` when `error_code` is a
numeric status and a plain `AuthException` when it is a code such as
`otp_expired`.
- `StorageApiException` is new and holds what a storage response reported,
including `fromJson`, which now takes a non-nullable status code.
- `PostgrestException` is always a response, so it mixes the api exception in
directly and its `statusCode` is required.
- `FunctionsApiException` is new and sits between `FunctionException` and the
relay/http subtypes. `FunctionsFetchException` has no status at all rather
than a null one.
Part of #1572 (tier 3), under the v3 umbrella #1278.
… unknown exceptions `AuthRetryableFetchException` and `AuthUnknownException` each declared a nullable `statusCode` of their own, so the auth hierarchy had three `statusCode` members at two different nullabilities while `AuthException` itself had none. Both fields were derived rather than load bearing: - `AuthRetryableFetchException` covers a transport failure and a 5xx alike, so it could never promise a status. When the service did answer, the message already carries what it said, falling back to the reason phrase or `HTTP <status>`. - `AuthUnknownException` computed the status from `originalError`, which it exposes, so callers can read it from the response directly. Neither is read anywhere in the client; the retry path only tests the type. `statusCode` now means one thing across every package: a non-nullable `int` that exists exactly when the exception is a `SupabaseApiException`.
…iException Dropping `statusCode` from `AuthRetryableFetchException` cost the status on a retryable failure that the service did answer. Two of the three throw sites put it in the message via `_getStatusMessage`, but the third takes the service's own text from a JSON error body, so the status was unreachable. `AuthRetryableApiException` extends `AuthRetryableFetchException` and mixes in `SupabaseApiException`, so the status is back with no nullable field: - `AuthRetryableFetchException` is the transport case, where the request never reached the service and there is no status to report. - `AuthRetryableApiException` is a 5xx the service answered with. `e is AuthRetryableFetchException` still catches both, so the retry and refresh paths in `GoTrueClient` are unchanged.
Most of the exception test files checked that Dart assigns constructor arguments to fields and that `extends` produces a subtype. Neither can fail. Removed: - Constructor round trips: build an exception, read back the values just passed in. Nine of these across the three files. - Type checks the compiler already guarantees, such as `expect(AuthApiException(...), isA<AuthException>())`, where the static type of the expression makes the matcher a tautology. The hierarchy test keeps its lists typed as `SupabaseException` so its checks stay real narrowings. - The whole `Error handling scenarios` group in gotrue: six tests restating per-class tests with different string literals. - `maintains equality based on parent class and reasons`, which compared field by field and never invoked `==`, so it would have passed with equality broken. - Per-subclass `toString` tests for subclasses that do not override it, which only re-asserted the base and mixin formats already covered in `supabase_common`. What is left covers behaviour that can break: the two `toString` formats, equality including the runtime type guard, the constructors that derive a message or an error code, `StorageApiException.fromJson`, and which exceptions are `SupabaseApiException`. The kept `toString` assertions are now exact matches instead of `contains`, so a format change fails them. gotrue's exception test drops from 40 tests to 17.
…rename PostgrestException Two alignment fixes from review. `FunctionsHttpException` and `FunctionsApiException` described the same thing: a non-2xx response from the Edge Function. That is the definition of an api exception, so `FunctionsApiException` takes over the name and the default message, and `FunctionsHttpException` is gone. `FunctionsRelayException` stays, since a relay error means the function may never have run. `PostgrestException` becomes `PostgrestApiException`. Every Postgrest failure comes from a response, so it was the only api exception not carrying the `Api` part, and the plain name is now free for a client raised base if one is ever needed. Also trims the exception dartdocs to what a caller needs, dropping the notes that explained the naming and the layering rationale.
dd6f916 to
2476ef4
Compare
… api base (#1699) Follows #1644, which introduced `SupabaseException` and `SupabaseApiException` and has landed. This PR only touches the Iceberg exceptions. Under the v3 umbrella #1278. Follows up on [@Vinzent03's question](#1644 (comment)) about why Iceberg was left out. It should not have been: it was the closest fit of anything not yet migrated, and it carried the same status code sentinel that #1644 removed from `FunctionsFetchException`. ## What `IcebergException` used `0` as the status code when a request never reached the catalog: ```dart final class IcebergNetworkException extends IcebergException { const IcebergNetworkException(super.message, {super.details}) : super(statusCode: 0); } ``` So a caller reading `statusCode` had to know that `0` meant "no response" rather than a real status. The sealed hierarchy now splits the same way as auth, storage and functions: ``` IcebergException message, errorCode, code, details ├── IcebergNetworkException no status, the request never went out └── IcebergApiException + statusCode <- SupabaseApiException ├── IcebergNotFoundException (404) ├── IcebergConflictException (409) ├── IcebergAuthenticationTimeoutException (419) ├── IcebergCommitStateUnknownException ├── IcebergServerException (5xx) └── IcebergUnknownException ``` `IcebergApiException` is itself `sealed`, so the six concrete subtypes stay closed and unchanged, and exhaustive switches over the hierarchy compile with exactly the same set of cases as before. ## Breaking changes | Before | After | | --- | --- | | `IcebergException.type` | `errorCode`, inherited from `SupabaseException` | | `IcebergException.statusCode` | `IcebergApiException.statusCode`; gone from the network case | | `IcebergException.statusCode == 0` | catch `IcebergNetworkException`, or check `is SupabaseApiException` | | `IcebergException.fromResponse` | `IcebergApiException.fromResponse` | `message`, `code` and `details` keep their names. `code` is still the Iceberg numeric error code, which is unrelated to `errorCode`, the error type string such as `NoSuchTableException`. `type` becomes `errorCode` because that is exactly what `SupabaseException.errorCode` is for, the same way `StorageException.error` became `errorCode` in #1644. The wire format is untouched: `fromResponse` still reads the `type` field out of the response body. ```dart // Before try { await catalog.loadTable(id); } on IcebergException catch (error) { if (error.statusCode == 0) { // the request never went out } print(error.type); } // After try { await catalog.loadTable(id); } on IcebergNetworkException catch (error) { // the request never went out print(error.details); } on IcebergApiException catch (error) { print('${error.statusCode}: ${error.errorCode}'); } ``` An Iceberg failure is now also reachable through the shared types, so a single catch covers it alongside any other service: ```dart try { await catalog.loadTable(id); } on SupabaseApiException catch (error) { print('${error.statusCode}: ${error.message}'); } ``` ## Testing `dart analyze`, `dcm analyze` and `dart format` are clean. The `storage_client` suite passes, including two new tests: one asserting that every subtype `fromResponse` can build is a `SupabaseApiException` while `IcebergNetworkException` is not, and one asserting that a network failure keeps the originating error in `details`. The existing error tests were updated for the renamed field and the moved factory. The capability matrix symbol, drift and schema checks pass with `IcebergApiException` registered. `MIGRATION.md` gains a section for the split, next to the one #1644 adds. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Breaking Changes** * Iceberg errors are now divided into network failures and API response failures. * API exceptions expose a required HTTP status code; network exceptions do not. * Replace `type` checks with the inherited `errorCode` property. * Update exception handling to catch the appropriate subtype. * Response parsing is now provided by `IcebergApiException.fromResponse`. * **Documentation** * Updated the migration guide with the new exception hierarchy and required caller changes. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Tier 3 of #1572, third of four PRs. Stacked on #1645 (which is stacked on #1644). ## What The same "double the delay, cap it" math existed in three places: | Site | Before | | --- | --- | | `RetryOptions.delay` (supabase_common) | `delayFactor * pow(2, min(attempt, 31)) * jitter`, capped at `maxDelay` | | `RetryTimer.createRetryFunction` (realtime_client) | `firstDelay << min(tries - 1, maxShift)`, capped at `maxDelay`, in milliseconds | | `PostgrestBuilder._defaultRetryDelay` | `Duration(seconds: min(pow(2, attempt), 30))` | All three now call one helper in `supabase_common`: ```dart Duration exponentialBackoff( int attempt, { required Duration initialDelay, required Duration maxDelay, double randomizationFactor = 0, Random? random, }); ``` `attempt` is zero based, so attempt `0` waits `initialDelay` and each attempt after that doubles until `maxDelay`. Jitter is opt-in per caller, since only the storage upload retry uses it today. ## No behaviour change Each call site keeps the delays it had: - realtime keeps its one-based `tries` and its millisecond return type, so `RealtimeClient(reconnectAfterMs: ...)` and the `TimerCalculation` typedef are untouched. It just passes `tries - 1` to the helper. - `RetryOptions.delay` keeps returning `Duration.zero` for attempt `0` and keeps its jitter. - postgrest still starts at one second and caps at thirty. `maxShift` (a public top-level const in `realtime_client`) is removed: the exponent clamp that stops the doubling from overflowing now lives with the shared calculation. It was not registered in the compliance matrix. Realtime's millisecond-based options (`reconnectAfterMs`, `heartbeatIntervalMs`, `longpollerTimeout`) would read better as `Duration`s, but converting them is a separate change; doing only `reconnectAfterMs` here would leave that API half converted. ## Testing New `backoff_test.dart` covers the start delay, the doubling, the cap, the absence of jitter by default and the jitter bounds with an injected `Random`. `realtime_client`'s existing `retry_timer_test.dart` and postgrest's `retry_test.dart` pass unchanged, which is the real check that the delays did not move. `melos analyze` and `melos format` clean; `gotrue`, `postgrest`, `storage_client`, `realtime_client`, `supabase` and `supabase_common` suites pass against the local stack. Capability matrix symbol and drift checks pass. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Improvements** - Standardized retry timing across PostgREST, Realtime, and shared client components. - Retry delays now increase exponentially, start at one second, and cap at 30 seconds. - Added bounded jitter support to help distribute repeated retry attempts more reliably. - Improved protection against overflow and invalid retry-attempt values. - **Tests** - Added coverage for delay growth, maximum limits, jitter behavior, defaults, and invalid inputs. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Tier 3 of #1572, last of four PRs. Stacked on #1646 (→ #1645 → #1644). The tier 3 note said the fetch wrappers are shareable in pieces, not as one wrapper, since each is bound to its own exception type. These are the pieces, now in `supabase_common`: | Helper | Replaces | | --- | --- | | `request.sendWith(httpClient)` | `httpClient != null ? httpClient.send(request) : request.send()` in functions, storage (3 sites) and the Iceberg catalog | | `headers.header(name)` | case-insensitive header lookups on a response | | `headers.mediaType` | functions' `(headers['Content-Type'] ?? headers['content-type'] ?? 'text/plain').split(';')[0].trim().toLowerCase()` and the Iceberg catalog's `contentType.contains('application/json')` | | `headers.redacted` | nothing; new, see below | | `tryDecodeJsonObject(body)` | the "decode the error body, fall back to the raw body" dance in storage and postgrest | `supabase_common` gains a dependency on `http` (`^1.6.0`, the constraint every other package already uses). There is deliberately no shared helper for the "set `Content-Type` unless the caller already did" check that storage and functions each had inline. `BaseRequest.headers` is already a case-insensitive map, so once the headers are on the request that check is just `request.headers.putIfAbsent('Content-Type', ...)`, which is what all three call sites now do. The Iceberg catalog moves to it as well, which drops an ordering subtlety there: it previously relied on `...?headers` spreading after a literal `'Content-Type'` key, so a caller who spelled the header with any other casing ended up with two entries rather than an override. ## Two behaviour changes Both follow from setting the content type on the request rather than on a detached map, and both are in functions' `invoke`: - A body given as a `String` now goes out as `text/plain; charset=utf-8` instead of a bare `text/plain`. `Request.body` fills the charset in itself, and the old header copy ran afterwards and overwrote it. - An explicit charset in a caller-supplied `Content-Type` now drives the body encoding, because the header is in place before `Request.body` reads it. Previously the body was always encoded as UTF-8 regardless of what the caller asked for. Relatedly, a multipart request no longer gets a content type from the switch on the body type. It generates its own boundary while being finalized and sets the header itself, so that default never reached the wire. ## Credentials in the request logs Raised in review. The logs interpolated the headers as sent, so `_log.finest` handed the caller's `apikey` and access token to whatever the logger writes to. `headers.redacted` replaces the value of every credential-bearing header with `<redacted>` and keeps the names, since knowing which headers went out is the useful part when reading a log. It is applied to the request logs and the `Initialize with headers:` logs in functions and storage. Those log lines date back to #1042, so this is a pre-existing leak rather than one this PR introduces, but it is fixed here since the shared helper it needs is what this PR is adding. ## Two bugs this surfaces Each has a regression test that fails without the fix. **Postgrest swallowed typed errors for `maybeSingle()`.** The decode and the `maybeSingle` handling shared one `try`/`catch (_)`: ```dart try { final errorJson = jsonDecode(response.body) as Map<String, dynamic>; error = PostgrestException.fromJson(...); if (_maybeSingle) { return _handleMaybeSingleError(response, error); // rethrows `error` for a real failure } } catch (_) { error = PostgrestException(message: response.body, ...); // ...and catches it here } ``` `_handleMaybeSingleError` returns null data for the "0 rows" case and rethrows otherwise, but that rethrow landed in the `catch (_)` and was replaced by a generic exception built from the raw body. So a `maybeSingle()` request that failed for a real reason lost its `errorCode`, `details` and `hint`. Splitting the decode from the handling fixes it. **A JSON error body with unexpected field types crashed.** Removing that catch-all exposed the next layer of the same problem: `PostgrestException.fromJson` cast `code` and `hint` to `String` and required a `String` message, so `{"code": 502, "message": "Bad gateway"}` — a shape gateways really do return — threw a `TypeError` out of the builder. Both `fromJson` factories now read every field defensively, so any JSON object yields an exception. `StorageException.fromJson` had the same casts and the same exposure. The matching storage bug, where a JSON error body that is not an object escaped as a `TypeError`, is fixed in #1644; here its inline guard collapses into `tryDecodeJsonObject`. ## Not converted Postgrest's own send site keeps managing its `Client` explicitly. It has to close a client it created even when the response body fails midway, and `BaseRequest.send` only closes its internal client when the stream completes normally. supabase_auth and postgrest also keep setting `Content-Type` unconditionally rather than defaulting it with `putIfAbsent`: both always JSON-encode the body, so honouring a caller-provided content type there would mislabel the request rather than respect it. The `Initialize with headers:` log in postgrest, realtime_client and supabase_auth still prints the caller's `apikey` and access token. Those packages are outside this refactor, and realtime's `params` map, which carries the `apikey` too, is a `Map<String, dynamic>`, so covering it needs a different shape of helper than the one on `Map<String, String>`. ## Testing New `http_test.dart` in `supabase_common` covers each helper, plus the three regression tests described above. Functions gains a test that a caller-supplied charset is what the body is actually encoded with, a test that no log record carries a credential value while every header name stays readable, and its existing `text/plain` assertion is tightened from `contains` to the full header value. `melos analyze` and `melos format` clean; `dcm analyze` passes (its only two hits are `avoid-inferrable-type-arguments` in `timestamp_test.dart`, which is unchanged from `main`). The `supabase_auth`, `postgrest`, `storage_client`, `functions_client`, `realtime_client`, `supabase`, `supabase_common`, `supabase_flutter`, `supabase_typegen` and `yet_another_json_isolate` suites pass. Capability matrix symbol and drift checks pass. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved handling of malformed, unexpected, or non-object API error responses. - Preserved error details, including status codes, messages, hints, codes, and additional information. - Improved request and response handling across Functions and Storage, including content types, media types, text encoding, and custom HTTP clients. - Redacted credentials from request logs. - **Tests** - Added coverage for malformed errors, multiple-row responses, custom clients, encoding, and HTTP utilities. - **Documentation** - Updated descriptions of shared HTTP functionality. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Tier 3 of #1572, first of four PRs. Under the v3 umbrella #1278.
What
AuthException,StorageException,PostgrestExceptionandFunctionExceptioneach reimplemented the same message plus status shape under different field names and types. They now share one hierarchy insupabase_common:A plain
SupabaseExceptionis a failure the client raised on its own, before or without a request. ASupabaseApiExceptionis a failure a service reported, sostatusCodeis a non-nullableintthat exists exactly when a service answered, and the layer a failure came from is visible from its type alone. Both are re-exported from every package, so one catch handles a failure from any service:The two level split came out of @Vinzent03's review. A single base handed every client raised failure a
statusCodeand anerrorCodethat were always null, and storage went further than that: a request that never reached the server producedStorageException(statusCode: 'ClientException'), the exception's own type name in the status field.SupabaseApiExceptionis a mixin rather than a second base class because the two axes are orthogonal.AuthApiExceptionhas to be anAuthException, since that is what callers catch, and also the cross service type for "a service answered". Dart gives you one superclass, so one of the two has to be a mixin or an interface, and neither can hold state. That is why each API exception declaresstatusCodeitself.The hierarchy
gotrueAuthException,AuthPKCEGrantCodeExchangeError,AuthSessionMissingException,AuthInvalidJwtException,AuthUnknownException,AuthRetryableFetchExceptionAuthApiException,AuthWeakPasswordException,AuthRetryableApiException(new)postgrestPostgrestApiExceptionstorage_clientStorageExceptionStorageApiException(new)functions_clientFunctionException,FunctionsFetchExceptionFunctionsApiException,FunctionsRelayExceptionPostgrestApiExceptionmixesSupabaseApiExceptionin directly instead of gaining a subclass, since every Postgrest failure comes from a response. It keeps theApipart of the name so it lines up with the other packages, which leaves the plainPostgrestExceptionname free for a client raised base if one is ever needed.Breaking changes
AuthException.statusCode(String?)AuthApiException.statusCode(int, required)AuthException.codeAuthException.errorCodeAuthSessionMissingException.statusCode == '400'errorCodeissession_missingAuthInvalidJwtException.statusCode == '400'errorCodeisinvalid_jwtAuthWeakPasswordException extends AuthExceptionextends AuthApiExceptionAuthRetryableFetchException.statusCodeAuthRetryableApiException, which carries itAuthUnknownException.statusCodeoriginalErrorStorageException.statusCode(String?)StorageApiException.statusCode(int, required)StorageException.errorerrorCodeStorageException.fromJson(json, '404')StorageApiException.fromJson(json, 404)PostgrestExceptionPostgrestApiExceptionPostgrestException.code(PostgREST code, or the HTTP status when the body was not JSON)errorCode(PostgREST or PostgreSQL code only) andstatusCode(HTTP status, non-nullable)PostgrestException.fromJson(json, code: 409)PostgrestApiException.fromJson(json, statusCode: 409)PostgrestException.toJson()keycodestatusCodeanderrorCodeFunctionsHttpExceptionFunctionsApiExceptionFunctionException.status(int)FunctionsApiException.statusCode(int, required)FunctionException.reasonPhrasemessageFunctionsFetchException.status == 0FunctionResponse.statusFunctionResponse.statusCodeNotes on the less mechanical ones:
code. It used to stuff the HTTP status intocodewhen the error body was not JSON, socodewas sometimesPGRST116and sometimes409. The status now has its own field anderrorCodeis only ever a PostgREST or PostgreSQL code. A duplicate key violation reads asstatusCode: 409, errorCode: '23505'.AuthSessionMissingExceptionandAuthInvalidJwtExceptionare raised by the client, never by the service, so the400they reported was invented. They report an error code instead.AuthRetryableFetchExceptionsplit in two. It was thrown both for a transport failure, which has no status, and for a 5xx, which has one. It now covers only the transport case, andAuthRetryableApiExceptionextends it for a 5xx.e is AuthRetryableFetchExceptionstill catches both, so the retry and refresh paths inGoTrueClientare unchanged.FunctionExceptionhad no message, onlystatus,detailsandreasonPhrase. The response's reason phrase becomes the message, and when the response carries none, as over HTTP/2, each subtype falls back to its own default, matching supabase-js:'Failed to send a request to the Edge Function','Relay error invoking the Edge Function','Edge Function returned a non-2xx status code'. The response body is still indetails.FunctionsHttpExceptionis gone. It andFunctionsApiExceptionboth meant "the Edge Function answered with a non-2xx status", which is the definition of an api exception, soFunctionsApiExceptiontook over the name and the default message.FunctionsRelayExceptionstays, because a relay error means the function may never have run.FunctionResponse.statusis renamed too. With the exceptions in that package reportingstatusCode, the successful response was the only thing left calling itstatus. It also lines up withhttp.Response.statusCode.AuthExceptioncompares the runtime type.statusCodemoved to the subclasses, so equality had to move with it. The base compares the concrete type, message and error code, and the API subclasses add the status, which keeps equality symmetric across the hierarchy.getSessionFromUrlalso had to change how it reads an error callback:error_codeholds either a numeric status (older links) or a code such asotp_expired. The numeric form now throws anAuthApiExceptionand anything else a plainAuthExceptionwhoseerrorCodefalls back to theerrorparameter. Both shapes are covered by tests.Every auth exception subclass used to repeat the same
toString; the shared classes now print the concrete runtime type, so only the subtypes with extra fields (originalError,reasons,details,hint) override it.A bug this surfaces
Fetch._handleErrorin storage cast the decoded error body toMap<String, dynamic>inside atry/on FormatException. A body that parses as JSON but is not an object, for example the["upstream connect error"]a gateway can return, throws aTypeErroron that cast, which theon FormatExceptiondoes not catch, so it escaped instead of surfacing as aStorageException. Fixed here, with a regression test; #1647 later replaces the inline guard with the sharedtryDecodeJsonObjecthelper.Testing
dart analyze,dcm analyzeanddart formatare clean. Full test suites pass forgotrue,postgrest,storage_client,functions_client,realtime_client,supabase,supabase_common(against the local stack) andsupabase_flutter, plus the examples analyzer. The capability matrix symbol, drift and schema checks pass with the new symbols registered.auth_exception_test.dartloses more lines than it gains, which is deliberate. It had grown a large number of tests that could not fail: constructor round trips reading back the values just passed in,isAchecks the compiler already guarantees, a wholeError handling scenariosgroup restating other tests with different string literals, and one test named after equality that compared field by field and never invoked==. What is left covers behaviour that can break, and itstoStringassertions are exact matches rather thancontains. The real coverage for this change is infetch_test.dart, which drives HTTP responses through the client and asserts the exception that comes out.Migration
MIGRATION.mdgains a section for the new hierarchy: the shape, the full rename table, and the one case that needs a code change rather than a rename, which is reading a status off a per-service base.Out of scope
RealtimeSubscribeExceptionhas nomessageat all, only aRealtimeSubscribeStatusanddetails, and it reports a channel subscription outcome rather than a request failure. It does not fit the base without inventing a message, so it keeps its own shape.The sealed
IcebergExceptionis a closer fit and is worth migrating, in its own PR: it already hasmessage, a non-nullablestatusCode, atypethat maps ontoerrorCode, anddetails. It also carries the samestatusCode == 0sentinel for a network failure that this PR removed fromFunctionsFetchException, so it wants the same client and api split rather than a straight rename.Making
FunctionExceptionsealed is tracked separately in #1550.