Skip to content

Commit b109ea0

Browse files
fix(storage): expose service error code on StorageApiError (#2537)
Co-authored-by: Katerina Skroumpelou <sk.katherine@gmail.com>
1 parent 2877b5b commit b109ea0

5 files changed

Lines changed: 60 additions & 2 deletions

File tree

‎packages/core/storage-js/src/lib/common/errors.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,38 @@ export function isStorageError(error: unknown): error is StorageError {
5858
export class StorageApiError extends StorageError {
5959
override status: number
6060
override statusCode: string
61+
/**
62+
* Service-specific error code from the Storage API response body, such as
63+
* `NoSuchKey`, `AccessDenied` or `ResourceAlreadyExists`. Use this to branch
64+
* on the specific error rather than parsing the message.
65+
* @see https://supabase.com/docs/guides/storage/debugging/error-codes
66+
*/
67+
code: string | undefined
6168

6269
constructor(
6370
message: string,
6471
status: number,
6572
statusCode: string,
66-
namespace: ErrorNamespace = 'storage'
73+
namespace: ErrorNamespace = 'storage',
74+
code?: string
6775
) {
6876
super(message, namespace, status, statusCode)
6977
this.name = namespace === 'vectors' ? 'StorageVectorsApiError' : 'StorageApiError'
7078
this.status = status
7179
this.statusCode = statusCode
80+
this.code = code
7281
}
7382

7483
toJSON(): {
7584
name: string
7685
message: string
7786
status: number | undefined
7887
statusCode: string | undefined
88+
code: string | undefined
7989
} {
8090
return {
8191
...super.toJSON(),
92+
code: this.code,
8293
}
8394
}
8495
}

‎packages/core/storage-js/src/lib/common/fetch.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ const handleError = async (
7676
.then(
7777
(err: { statusCode?: string; code?: string; error?: string; message?: string } | null) => {
7878
const statusCode = err?.statusCode || err?.code || status + ''
79-
reject(new StorageApiError(_getErrorMessage(err), status, statusCode, namespace))
79+
reject(
80+
new StorageApiError(_getErrorMessage(err), status, statusCode, namespace, err?.code)
81+
)
8082
}
8183
)
8284
.catch(() => {

‎packages/core/storage-js/test/common/errors.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ describe('Common Errors', () => {
7979
statusCode: 'InternalError',
8080
})
8181
})
82+
83+
it('should expose the service error code when provided', () => {
84+
const error = new StorageApiError('Object not found', 404, '404', 'storage', 'NoSuchKey')
85+
expect(error.code).toBe('NoSuchKey')
86+
expect(error.statusCode).toBe('404')
87+
})
88+
89+
it('should leave code undefined when not provided', () => {
90+
const error = new StorageApiError('API error', 404, 'NotFound')
91+
expect(error.code).toBeUndefined()
92+
})
93+
94+
it('should include code in JSON when present', () => {
95+
const error = new StorageApiError('Access denied', 403, '403', 'storage', 'AccessDenied')
96+
expect(error.toJSON()).toEqual({
97+
name: 'StorageApiError',
98+
message: 'Access denied',
99+
status: 403,
100+
statusCode: '403',
101+
code: 'AccessDenied',
102+
})
103+
})
82104
})
83105

84106
describe('StorageUnknownError', () => {

‎packages/core/storage-js/test/common/fetch.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,24 @@ describe('Common Fetch', () => {
9898
})
9999
})
100100

101+
it('should expose the service error code from the response body', async () => {
102+
// Mirror a real Storage payload, which sends both `statusCode` and `code`
103+
const errorResponse = { statusCode: '404', code: 'NoSuchKey', message: 'Object not found' }
104+
mockFetch.mockResolvedValue(
105+
new MockResponse(JSON.stringify(errorResponse), {
106+
status: 404,
107+
statusText: 'Not Found',
108+
})
109+
)
110+
111+
await expect(get(mockFetch, 'http://test.com/api')).rejects.toMatchObject({
112+
name: 'StorageApiError',
113+
status: 404,
114+
statusCode: '404',
115+
code: 'NoSuchKey',
116+
})
117+
})
118+
101119
it('should throw StorageUnknownError on network error', async () => {
102120
mockFetch.mockRejectedValue(new Error('Network error'))
103121

‎sdk-compliance.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,11 @@ features:
554554
status: implemented
555555
symbols:
556556
- default.listBuckets
557+
storage.errors.error_codes:
558+
status: implemented
559+
symbols:
560+
- StorageApiError.code
561+
- StorageVectorsApiError.code
557562
storage.file_buckets.access_bucket:
558563
status: implemented
559564
symbols:

0 commit comments

Comments
 (0)