Error Handling
The SettleSettle SDK uses typed error classes. Always catch by class — never by message string, because messages can change between versions.
Import the error classes you need
typescript
import {
SettleSettleError, // Base — catch all SDK errors
InsufficientCreditsError, // 402 — user has no credits
AuthenticationError, // 401 — bad or missing API key
ValidationError, // 400 — invalid request body
RateLimitError, // 429 — too many requests
TimeoutError, // 408 — request took too long
ApiError, // Any other API error
} from 'settlesettle'Full handling example
typescript
try {
await settle.wallet.debit('user_123', { amount: 10 })
} catch (err) {
if (err instanceof InsufficientCreditsError) {
// Expected — user is out of credits
// Trigger your paywall or ad fallback
console.log(`Need: ${err.requestedAmount}, Have: ${err.currentBalance}`)
} else if (err instanceof AuthenticationError) {
// Your API key is wrong or missing
console.error('Check your SETTLESETTLE_API_KEY.')
} else if (err instanceof ValidationError) {
// You sent a bad request body
console.error('Bad request:', err.message, err.fields)
} else if (err instanceof RateLimitError) {
// Slow down — too many requests
console.error(`Rate limited. Retry in ${err.retryAfterMs}ms.`)
} else if (err instanceof TimeoutError) {
// Request took too long
console.error('Request timed out. Check your connection.')
} else if (err instanceof SettleSettleError) {
// Any other SDK error — catch-all
console.error(`SDK error [${err.code}]: ${err.message}`)
console.error('Status code:', err.statusCode)
} else {
// Completely unexpected — rethrow
throw err
}
}Error class reference
| Class | HTTP Status | When it's thrown | Key properties |
|---|---|---|---|
SettleSettleError | Any | Base class — don't throw directly | statusCode, code, payload |
AuthenticationError | 401 | API key is missing, wrong, or expired | — |
InsufficientCreditsError | 402 | Wallet debit with not enough credits | userId, currentBalance, requestedAmount |
ValidationError | 400 | Invalid request payload | fields (which fields failed) |
RateLimitError | 429 | Too many requests | retryAfterMs |
TimeoutError | 408 | Request exceeded timeout | — |
ApiError | Other | Any unhandled server error | statusCode |
Errors that are retried automatically by the SDK:
The SDK automatically retries these before throwing — you only see the error if all retries are exhausted:
429 RateLimitError— respectsRetry-Afterheader5xxserver errors- Timeouts
- Network failures
These are never retried (they're deterministic failures):
400 ValidationError401 AuthenticationError402 InsufficientCreditsError403 ForbiddenError
