Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,24 @@ const storageClient = new StorageClient(STORAGE_URL, {
const { data, error } = await storageClient.from('public-bucket').getPublicUrl('path/to/file')
```

- Check if a file exists:

```js
const { data, error } = await storageClient.from('bucket').exists('path/to/file')
// data will be true if the file exists, false otherwise
```

### Error Handling

Supplying `.throwOnError()` will throw errors instead of returning them as a property on the response object.

```js
try {
const { data } = await storageClient
.from('bucket')
.throwOnError()
.download('path/to/file')
} catch (error) {
console.error(error)
}
```
```js
try {
const { data } = await storageClient.from('bucket').throwOnError().download('path/to/file')
} catch (error) {
console.error(error)
}
```

## Sponsors

Expand Down
18 changes: 13 additions & 5 deletions src/packages/StorageFileApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isStorageError, StorageError, StorageUnknownError } from '../lib/errors'
import { isStorageError, StorageError, StorageUnknownError, StorageApiError } from '../lib/errors'
import { Fetch, get, head, post, put, remove } from '../lib/fetch'
import { recursiveToCamel, resolveFetch } from '../lib/helpers'
import {
Expand Down Expand Up @@ -604,11 +604,19 @@ export default class StorageFileApi {
if (this.shouldThrowOnError) {
throw error
}
if (isStorageError(error) && error instanceof StorageUnknownError) {
const originalError = (error.originalError as unknown) as { status: number }
if (isStorageError(error)) {
// Check for both StorageApiError (which has status directly) and StorageUnknownError (with originalError)
let status: number | undefined

if (error instanceof StorageApiError) {
status = error.status
} else if (error instanceof StorageUnknownError) {
const originalError = (error.originalError as unknown) as { status: number }
status = originalError?.status
}

if ([400, 404].includes(originalError?.status)) {
return { data: false, error }
if (status && [400, 404].includes(status)) {
return { data: false, error: null }
}
}

Expand Down