Skip to main content
Version: 4.x.x

Error Handling

Errors are returned as the second element of the tuple returned from the request.

info

Even though requests are promises, they are never rejected. Due to the format they return, any error will appear as the second tuple element and for this reason we can always expect resolved status.

const { error } = await request.send();

Global errors

Global errors are the general type of error returned from each query within a given client. We can set their type by giving the appropriate generic to the instance of our client.

type GlobalErrorType = {
message: string;
status: 400 | 404 | 500;
};

export const client = new Client<GlobalErrorType>({ url });

Now the error type of our request will reflect GlobalErrorType.

const { error } = await request.send();

console.log(error); // null | GlobalErrorType;

Local errors

Local errors are representatives of individual error types returned only on particular endpoints. The best example are form endpoints, we often encounter errors returned from the server related to individual form fields.

import { client } from "./client";

type LocalErrorType = {
errors: {
name?: string;
email?: string;
age?: string;
};
};

const postUser = client.createRequest<ResponseType, RequestType, LocalErrorType>()({
method: "POST",
endpoint: "/users",
});

Now the error type of our request will union of GlobalError and LocalErrorType.

const { error } = await request.send();

console.log(error); // null | GlobalErrorType | LocalErrorType;