fix(storage): correct exists() error handling for non-existent files #1745
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
authored by @SumitKumar-17
Transfered from: supabase/storage-js#256
Bug Fix Report
I have successfully identified and fixed the bug reported in the
issue. Here's what I found and resolved:
The Issue
The bug report was partially correct. The
exists
method was indeedpresent in both browser and Node.js builds (I confirmed it existed
in the compiled output), but there was an error handling bug that
made it appear to not work properly in certain cases.
The Root Cause
The problem was in the error handling logic of the
exists
method insrc/packages/StorageFileApi.ts
. When a file doesn't exist(resulting in a 404 HTTP status), the method would return
{ data: false, error: StorageError }
instead of{ data: false, error: null }
.This was incorrect behavior because:
operation failed
{ data: false, error: null }
to indicate"file doesn't exist, but the check was successful"
The Fix
I updated the
exists
method to properly handle bothStorageApiError
(which contains the status code directly) andStorageUnknownError
(which contains the original error object),and return
{ data: false, error: null }
when the error status is400 or 404.
Changes Made
exists
method to properly return{ data: false, error: null }
when a file doesn't existStorageApiError
exists
methodVerification
Solves: #1600