Skip to content

Commit 7bcaa60

Browse files
committed
Add statusCode to StorageApiError to preserve pre-change behavior
1 parent 68173ee commit 7bcaa60

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

src/lib/errors.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ export function isStorageError(error: unknown): error is StorageError {
1313

1414
export class StorageApiError extends StorageError {
1515
status: number
16+
statusCode: string
1617

17-
constructor(message: string, status: number) {
18+
constructor(message: string, status: number, statusCode: string) {
1819
super(message)
1920
this.name = 'StorageApiError'
2021
this.status = status
22+
this.statusCode = statusCode
2123
}
2224

2325
toJSON() {
2426
return {
2527
name: this.name,
2628
message: this.message,
2729
status: this.status,
30+
statusCode: this.statusCode,
2831
}
2932
}
3033
}

src/lib/fetch.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ const handleError = async (
2727
error
2828
.json()
2929
.then((err) => {
30-
reject(new StorageApiError(_getErrorMessage(err), error.status || 500))
30+
const status = error.status || 500
31+
const statusCode = err?.statusCode || status + ''
32+
reject(new StorageApiError(_getErrorMessage(err), status, statusCode))
3133
})
3234
.catch((err) => {
3335
reject(new StorageUnknownError(_getErrorMessage(err), err))

src/packages/StorageFileApi.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,7 @@ export default class StorageFileApi {
200200
headers['content-type'] = options.contentType as string
201201
}
202202

203-
const data = await put(
204-
this.fetch,
205-
url.toString(),
206-
body as object,
207-
{ headers }
208-
)
203+
const data = await put(this.fetch, url.toString(), body as object, { headers })
209204

210205
return {
211206
data: { path: cleanPath, fullPath: data.Key },

test/storageFileApi.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ describe('Object API', () => {
206206
const outError = res.error as StorageApiError
207207
expect(outError).toBeInstanceOf(StorageApiError)
208208
expect(outError.message).toBe('The object exceeded the maximum allowed size')
209+
expect(outError.statusCode).toBe('413')
209210
})
210211

211212
test('can upload a file with a valid mime type', async () => {
@@ -234,6 +235,7 @@ describe('Object API', () => {
234235
const outError = res.error as StorageApiError
235236
expect(outError).toBeInstanceOf(StorageApiError)
236237
expect(outError.message).toBe('mime type image/jpeg is not supported')
238+
expect(outError.statusCode).toBe('415')
237239
})
238240

239241
test('sign url for upload', async () => {
@@ -299,6 +301,7 @@ describe('Object API', () => {
299301
const outError = uploadRes2.error as StorageApiError
300302
expect(outError).toBeInstanceOf(StorageApiError)
301303
expect(outError.message).toBe('The resource already exists')
304+
expect(outError.statusCode).toBe('409')
302305
})
303306
})
304307

0 commit comments

Comments
 (0)