Error handling
HTTP errors do not throw
SDK service calls behave like fetch: they resolve with { status, result } for every HTTP response, including errors.
const { status, result } = await customers.OU.create({ data: { name: 'x' } })
if (status === 400) {
console.log(result) // { errors: [{ message: '…', path: 'name' }] }
}
const ok = ({ status }) => status >= 200 && status < 300
const orThrow = (response, label) => {
if (ok(response)) return response.result
const detail = response.result?.error ?? JSON.stringify(response.result?.errors)
throw new Error(`${label} failed (${response.status}): ${detail}`)
}
const company = orThrow(await OU.create({ data }, { return: true }), 'create company')
Every script in the test data uses this expectOk pattern.
What throws
The SDK throws for problems that never reach a server response:
| Error | When |
|---|---|
sdk/fetch-failed | Network failure (DNS, connection refused, TLS) |
sdk/endpoint-invalid | A 404 whose body is not JSON, typically a wrong host |
sdk/non-json-response | The body is not JSON (proxy or gateway page) |
oauth/token-request-failed: <status> <body> | The token endpoint rejected your app |
auth/exchange-failed: <status> <body> | User token exchange failed |
sdk/…, customers/…, insights/… | Argument checks before sending (missing humanId, empty batch, wrong filter type, …) |
Server response formats
| Service | Format |
|---|---|
| Customers, Insights | { "error": "entity/not-found" } or, for validation, { "errors": [{ "message": "…", "path": "…" }] } |
| Auth | Plain text body such as oauth/invalid-client |
| Any 401 from Customers | Empty body |
Error codes look like scope/problem. The full list is in the error reference.
Retries
The SDK has no retries, backoff or timeouts. Add your own for transient failures:
const retry = async (fn, { tries = 3, delayMs = 500 } = {}) => {
for (let attempt = 1; ; attempt++) {
try {
const response = await fn()
if (response.status < 500 || attempt === tries) return response
} catch (error) {
if (attempt === tries || error.message !== 'sdk/fetch-failed') throw error
}
await new Promise(resolve => setTimeout(resolve, delayMs * attempt))
}
}
const response = await retry(() => orders.Doc.create(doc))
Only retry idempotent calls. Insights Doc.create is idempotent only if the source's duplicate control is set to a field. Customers create is not.
throwOnError
The service classes accept { throwOnError: true } and then throw for any non-2xx response. The error message is result.error (or sdk/request-failed), with errors[].message appended, and error.status carries the HTTP status. The public sdk.asAdmin() and sdk.asUser() contexts do not expose this switch.