Error Handling
How to handle API errors gracefully in your integration.
StoneDock API error bodies are a flat JSON object, not a nested error object. The error field
itself is always the short, stable, snake_case string to branch your code on — message is
present on many but not all responses, and is for logs/humans, not branching logic:
{
"error": "invalid_status",
"message": "Requested scope(s) are not available in Phase 1.",
"allowed": ["draft", "active", "on-hold"]
}A 422 validation failure instead carries a details array:
{
"error": "processing_failed",
"details": [
{ "field": "externalId", "issue": "required" }
]
}The SDKs normalize all of this into typed error classes so you don't need to pattern-match the raw body yourself — see below.
Handling by Status Code
401 — Unauthorized
Your API key is missing/invalid, or your consent token is missing, invalid, or expired.
try {
const user = await onepath.users.onboard({ ... });
} catch (err) {
if (err instanceof OnepathAuthError) {
// Refresh your consent token and retry
const newToken = await generateConsentToken(userId);
// retry with newToken
}
}403 — Forbidden
Your API key is valid, but the requested scope isn't granted to your partner application (or, for consent-gated endpoints, consent was revoked). Re-requesting won't help — you need the scope granted first.
} catch (err) {
if (err instanceof OnepathForbiddenError) {
// err.message describes which scope is missing — check your Partner Portal grants
console.error(err.message);
}
}429 — Rate Limited
You've exceeded 100 requests/minute. Respect the Retry-After header.
} catch (err) {
if (err instanceof OnepathRateLimitError) {
const retryAfter = err.retryAfter; // seconds
await sleep(retryAfter * 1000);
// retry
}
}5xx — Server Errors
Safe to retry with exponential backoff. Use jitter to avoid thundering herds.
async function withRetry<T>(fn: () => Promise<T>, maxAttempts = 3): Promise<T> {
for (let attempt = 0; attempt < maxAttempts; attempt++) {
try {
return await fn();
} catch (err) {
if (!(err instanceof OnepathServerError) || attempt === maxAttempts - 1) throw err;
await sleep(Math.pow(2, attempt) * 1000 + Math.random() * 500);
}
}
throw new Error("Max retries exceeded");
}