HttpError
HttpError is the error type the client throws (rejects with) whenever a request fails with a non-2xx status. It is re-exported from @toapi/client for convenience, but the class itself is defined in @toapi/common and is the same class the server throws — so you can instanceof-check it consistently on both sides.
import { HttpError } from "@toapi/client";class HttpError<Data = any> extends Error { status: number; data?: Data;
constructor(status: number, message: string, data?: Data);}status— the HTTP status code (e.g.404,409,500).message— the error message (from the server’s JSON error payload, or the response’sstatusText).data— an optional structured payload the server attached to the error.
How the client produces it
Section titled “How the client produces it”When a response is not 200:
- If the response carries
Content-Type: application/json+httperror, the client reads the JSON{ message, data }and throwsnew HttpError(status, message, data). - Otherwise it throws
new HttpError(status, statusText). - As a special case, a
400withContent-Type: application/json+zodissuesis thrown as aZodError(fromzod) instead, so you can inspect validation issues directly.
Handling errors
Section titled “Handling errors”import { HttpError } from "@toapi/client";
try { await client.users["999"].get();} catch (error) { if (error instanceof HttpError) { console.error(`Request failed (${error.status}): ${error.message}`); if (error.status === 404) { // handle not found } // error.data holds any structured payload the server sent } else { throw error; }}