Skip to content

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’s statusText).
  • data — an optional structured payload the server attached to the error.

When a response is not 200:

  • If the response carries Content-Type: application/json+httperror, the client reads the JSON { message, data } and throws new HttpError(status, message, data).
  • Otherwise it throws new HttpError(status, statusText).
  • As a special case, a 400 with Content-Type: application/json+zodissues is thrown as a ZodError (from zod) instead, so you can inspect validation issues directly.
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;
}
}