TypeScript
Errors
Handle typed API failures, timeouts, and raw HTTP responses
Overview
This guide explains how to handle errors returned by the Insion SDK and inspect response details when troubleshooting failed requests.
Handle errors
All SDK failures extend InsionError. Known API statuses also have exported errors such as BadRequestError, UnauthorizedError, ForbiddenError, and ServiceUnavailableError; timeouts use InsionTimeoutError.
import { Insion, InsionError, InsionTimeoutError } from "@insion/client";
try {
await client.moderateARecord({
clientId: "post-123",
name: "A post",
entity: "post",
content: "Hello",
});
} catch (error) {
if (error instanceof Insion.BadRequestError) {
console.error(error.body);
} else if (error instanceof InsionTimeoutError) {
console.error("Insion timed out");
} else if (error instanceof InsionError) {
console.error(error.statusCode, error.body);
}
throw error;
}Inspect raw responses
Calls return HttpResponsePromise. Call .withRawResponse() before awaiting when you need headers or the status code.
const { data, rawResponse } = await client.listRecords({ limit: 20 }).withRawResponse();
console.log(data.has_more, rawResponse.status, rawResponse.headers);How is this guide?