Skip to content

fix(storage): use toString() for statusCode to handle non-string type…#1323

Open
mitchkoko wants to merge 1 commit intosupabase:mainfrom
mitchkoko:fix/storage-exception-status-code
Open

fix(storage): use toString() for statusCode to handle non-string type…#1323
mitchkoko wants to merge 1 commit intosupabase:mainfrom
mitchkoko:fix/storage-exception-status-code

Conversation

@mitchkoko
Copy link
Copy Markdown

…s StorageException.fromJson used 'as String?' to cast statusCode, which

returns null when the server sends statusCode as an integer. Changed to
.toString() so the statusCode from the JSON body is correctly preserved
regardless of type.

Fixes #1312

What kind of change does this PR introduce?

Bug fix

What is the current behavior?

StorageException.fromJson uses as String? to cast json['statusCode'], which silently returns null when the server sends statusCode as an integer (e.g., 404 instead of "404"). This causes the exception to fall back to the HTTP status code, which can be different from the actual storage error code.

For example, a "not found" error returns statusCode: 400 on the exception object, while the JSON body contains 404.

What is the new behavior?

Uses .toString() instead of as String?, which correctly converts the statusCode regardless of whether the server sends it as a string or integer. The exception now consistently reflects the status code from the JSON response body.

Additional context

Updated test expectations from '400' to '404' in three places where tests were asserting the old (incorrect) status code for "file not found" scenarios.

…s StorageException.fromJson used 'as String?' to cast statusCode, which

  returns null when the server sends statusCode as an integer. Changed to
  .toString() so the statusCode from the JSON body is correctly preserved
  regardless of type.

  Fixes supabase#1312
@grdsdev
Copy link
Copy Markdown
Contributor

grdsdev commented Mar 24, 2026

Hey @mitchkoko, I understand the change and it makes sense, but tests are failing.

The issue arises because the StorageException is initialized through the following code:

https://github.com/supabase/supabase-flutter/blob/main/packages/storage_client/lib/src/fetch.dart#L38-L40

However, it doesn't use fromJson, which is where the change was made.

This happens because, even though the JSON response contains statusCode = 404, the HTTP response status code remains 400. I know this isn’t ideal, but it’s how the storage service currently works.

Please revert the test changes, but keep the toString() since it does make sense.

Thanks.

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.

Mismatch status code in StorageException object and in exception message body.

2 participants