Errors and recovery
Handle SDK exceptions, HTTP failures, read retries, cancellation, and uncertain write outcomes.
Use try/catch around await calls. Local validation can throw before a Promise is returned for some methods, so try/catch also covers that case. Keep remote scan state separate from request errors: a failed status refresh does not mean a scan failed.
Error classes
All exported SDK exceptions extend StraylabsError.
| Class | Meaning |
|---|---|
StraylabsInputError | Invalid configuration, project type, strict blackbox input, identifier, filename or serializable body |
StraylabsHttpError | Non-success HTTP response; inspect status, detail, body, correlationId and retryAfterMs |
StraylabsTransportError | Request or response-body transfer failed; includes a sanitized cause |
StraylabsTimeoutError | The operation deadline expired, including read retries, delays and body reads |
StraylabsAbortError | The caller cancelled local work |
StraylabsProtocolError | Invalid success status/body, incompatible required shape, invalid lookup metadata or unexpected report content type |
StraylabsOrganizationLookupError | Name lookup has reason: "not_found" or "ambiguous"; includes organizationName |
Request errors carry method, API-relative route, known resource ref, and outcomeUnknown. Fields can be absent when validation failed before request context existed. Do not handle business restrictions by matching English message text. HTTP error details may be a string, validation array, other JSON, or a plain proxy response.
Catch errors without blindly retrying writes
import {
StraylabsAbortError,
StraylabsError,
StraylabsHttpError,
StraylabsInputError,
StraylabsOrganizationLookupError,
StraylabsProtocolError,
StraylabsTimeoutError,
StraylabsTransportError,
} from "@straylabs/sdk";
export function classifyError(error: unknown) {
if (!(error instanceof StraylabsError)) {
return { action: "investigate-application-error" as const };
}
if (error.outcomeUnknown) {
return { action: "reconcile-write" as const, method: error.method, route: error.route };
}
if (error instanceof StraylabsOrganizationLookupError) {
return { action: "select-organization" as const, reason: error.reason };
}
if (error instanceof StraylabsInputError) {
return { action: "correct-input" as const };
}
if (error instanceof StraylabsAbortError) {
return { action: "cancelled-locally" as const };
}
if (error instanceof StraylabsProtocolError) {
return { action: "investigate-api-contract" as const };
}
if (error instanceof StraylabsHttpError) {
return {
action: "handle-http-failure" as const,
status: error.status,
correlationId: error.correlationId,
retryAfterMs: error.retryAfterMs,
};
}
if (error instanceof StraylabsTimeoutError || error instanceof StraylabsTransportError) {
return { action: "schedule-read-or-reconcile-write" as const };
}
return { action: "investigate-sdk-error" as const };
}
try {
const scan = await client.scans.get(projectRef);
// Save or return the current state. null means the project has no scans.
} catch (error) {
const handling = classifyError(error);
// Persist the selected action in your application's request/synchronization state.
throw error;
}The returned actions above are application examples, not SDK methods or backend error codes. The outcomeUnknown check comes first because a timeout, abort, protocol failure, transport failure, or HTTP 5xx after a dispatched write may require reconciliation rather than immediate repetition.
Interpret HTTP statuses
| Status | Handle as |
|---|---|
| 400 | Business/input restriction, for example a duplicate project name, plan capacity or target policy |
| 401 | Invalid, expired or revoked credential |
| 403 | Agreement acceptance, resource permission or tier restriction; preserve the actual detail |
| 404 | Missing project, scan or explicitly requested file |
| 409 | Scan-control conflict; read the current state before another action |
| 422 | Request validation; details may be a sentence or structured array |
| 429 | Rate limit; honor retryAfterMs and coordinate requests sharing the key |
| 5xx | Provider/proxy/queue failure; a write may already have taken effect |
An empty scan history becomes null in latest-scan lookup. No ready PDF or a selected latest PDF disappearing becomes null in default report download. Permission errors and other request failures are not converted to absence. Explicit scan/file 404s remain HTTP errors.
Automatic read retries
By default, GET network failures and HTTP 429/502/503/504 receive at most two additional attempts. Backoff uses jitter with an initial one-second exponential component capped at 30 seconds. A valid delta-seconds or HTTP-date Retry-After is a minimum delay.
All attempts and waits fit within the operation timeout. When the next delay cannot fit, the call raises StraylabsTimeoutError. Authentication, validation, protocol errors, caller cancellation and POST requests do not receive automatic retries.
The application can schedule another bounded read later under its own retry policy. Do not add a generic retry wrapper around project creation, scan start/stop or report generation.
Cancellation and deadlines
const controller = new AbortController();
const pending = client.scans.get(projectRef, {
signal: controller.signal,
timeoutMs: 10_000,
});
// Call this when your caller cancels or disconnects.
controller.abort();
try {
await pending;
} catch (error) {
if (!(error instanceof StraylabsAbortError)) throw error;
}Latest scan lookup and latest report download each share one timeout across their lookup and detail/file requests. Positive timeouts must be at most 2,147,483,647 ms. Cancelling a request never stops a remote scan; scans.stop(projectRef) is an explicit write with its own outcome.
Recover an uncertain mutation
Persist an intent before sending a POST and checkpoint success immediately. There is no documented SDK idempotency-key mechanism or automatic remote rollback.
| Interrupted operation | Read-based recovery |
|---|---|
| Project creation | List projects and match a deterministic name; verify targets, scope, type and account settings before adopting one unique match |
| Scan start | Compare scan history against saved pre-start IDs; adopt only a uniquely identified matching execution |
| Report generation | Compare files with the pre-generation inventory; do not repeatedly enqueue generation to recover a lost job ID |
| Scan stop | Read the saved scan and the project's current pointer before considering another control request |
Zero or multiple matches require reconciliation. A name is not an idempotency guarantee. A crashed process may have received success before saving the checkpoint, so use the same recovery rules after restart. Do not delete a remote project/scan as automatic rollback.
Use durable application locking for concurrent launch/refresh/report work. Keep keys in server secrets and avoid logging complete error bodies, target credentials, findings, reproduction text or report bytes. Log selected metadata such as error type, HTTP status and correlation ID under your application's data policy.
Last updated