sobj

Errors โ€‹

All errors thrown by Sobj are instances of SobjError or one of its subclasses.

๐Ÿ“ฅ Import โ€‹

ts
import {
  SobjError,
  SobjNotFoundError,
  SobjPermissionError,
  SobjConflictError,
  SobjInvalidRequestError,
  SobjNetworkError,
  isSobjError,
} from 'sobj/errors'

๐Ÿชน SobjError โ€‹

Base error class. All Sobj errors extend this.

ts
class SobjError extends Error {
  readonly code: SobjErrorCode
  readonly status?: number
  readonly requestId?: string
}

๐Ÿ“‹ Properties โ€‹

PropertyTypeDescription
codeSobjErrorCodeError code
statusnumber | undefinedHTTP status code, if applicable
requestIdstring | undefinedRequest ID from the provider, if available
messagestringError message
causeunknownOriginal error, if wrapped

๐Ÿ“‹ SobjErrorCode โ€‹

ts
type SobjErrorCode =
  | 'NOT_FOUND'
  | 'PERMISSION_DENIED'
  | 'CONFLICT'
  | 'INVALID_REQUEST'
  | 'NETWORK_ERROR'
  | 'UNKNOWN'

๐Ÿ” SobjNotFoundError โ€‹

Thrown when an object does not exist.

ts
class SobjNotFoundError extends SobjError {
  // code: 'NOT_FOUND'
}

Note: get and head return null instead of throwing SobjNotFoundError. 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.

ts
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).

ts
class SobjConflictError extends SobjError {
  // code: 'CONFLICT'
}

๐Ÿงฉ SobjInvalidRequestError โ€‹

Thrown when the request is malformed or uses invalid parameters.

ts
class SobjInvalidRequestError extends SobjError {
  // code: 'INVALID_REQUEST'
}

๐ŸŒ SobjNetworkError โ€‹

Thrown when the underlying network request fails (before a response is received).

ts
class SobjNetworkError extends SobjError {
  // code: 'NETWORK_ERROR'
}

๐Ÿ‘ฎ isSobjError โ€‹

Type guard that checks whether a value is a SobjError.

ts
function isSobjError(error: unknown): error is SobjError

๐Ÿงช Example โ€‹

ts
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:

ts
const object = await storage.get('file.txt')

if (object === null) {
  // doesn't exist
}

๐ŸŽฏ Catching specific errors โ€‹

ts
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
  }
}