Errors โ
All errors thrown by Sobj are instances of SobjError or one of its subclasses.
๐ฅ Import โ
import {
SobjError,
SobjNotFoundError,
SobjPermissionError,
SobjConflictError,
SobjInvalidRequestError,
SobjNetworkError,
isSobjError,
} from 'sobj/errors'๐ชน SobjError โ
Base error class. All Sobj errors extend this.
class SobjError extends Error {
readonly code: SobjErrorCode
readonly status?: number
readonly requestId?: string
}๐ Properties โ
| Property | Type | Description |
|---|---|---|
code | SobjErrorCode | Error code |
status | number | undefined | HTTP status code, if applicable |
requestId | string | undefined | Request ID from the provider, if available |
message | string | Error message |
cause | unknown | Original error, if wrapped |
๐ SobjErrorCode โ
type SobjErrorCode =
| 'NOT_FOUND'
| 'PERMISSION_DENIED'
| 'CONFLICT'
| 'INVALID_REQUEST'
| 'NETWORK_ERROR'
| 'UNKNOWN'๐ SobjNotFoundError โ
Thrown when an object does not exist.
class SobjNotFoundError extends SobjError {
// code: 'NOT_FOUND'
}Note:
getandheadreturnnullinstead of throwingSobjNotFoundError. This error is thrown in other contexts (e.g. when a copy source is missing).
๐ SobjPermissionError โ
Thrown when the request is rejected due to insufficient permissions.
class SobjPermissionError extends SobjError {
// code: 'PERMISSION_DENIED'
}๐ฅ SobjConflictError โ
Thrown when an operation conflicts with the current state of an object (e.g. conditional write failure).
class SobjConflictError extends SobjError {
// code: 'CONFLICT'
}๐งฉ SobjInvalidRequestError โ
Thrown when the request is malformed or uses invalid parameters.
class SobjInvalidRequestError extends SobjError {
// code: 'INVALID_REQUEST'
}๐ SobjNetworkError โ
Thrown when the underlying network request fails (before a response is received).
class SobjNetworkError extends SobjError {
// code: 'NETWORK_ERROR'
}๐ฎ isSobjError โ
Type guard that checks whether a value is a SobjError.
function isSobjError(error: unknown): error is SobjError๐งช Example โ
import { isSobjError, SobjNotFoundError } from 'sobj/errors'
try {
await storage.copy('missing.txt', 'dest.txt')
} catch (error) {
if (error instanceof SobjNotFoundError) {
console.log('Source object not found')
} else if (isSobjError(error)) {
console.log('Storage error:', error.code, error.status)
} else {
throw error
}
}โ๏ธ Error handling patterns โ
๐ก๏ธ Null-safe operations โ
get and head return null when an object does not exist, so they rarely throw:
const object = await storage.get('file.txt')
if (object === null) {
// doesn't exist
}๐ฏ Catching specific errors โ
import {
SobjPermissionError,
SobjNetworkError,
isSobjError,
} from 'sobj/errors'
try {
await storage.put('file.txt', body)
} catch (error) {
if (error instanceof SobjPermissionError) {
// handle auth failure
} else if (error instanceof SobjNetworkError) {
// handle connectivity problem
} else if (isSobjError(error)) {
// handle other Sobj errors
console.log(error.code, error.message)
} else {
throw error
}
}