wasp/errors
HttpError, the error for HTTP error responses, and its types.
You can import wasp/errors in both server and client code.
Classes
HttpError
An error for an HTTP error response, which has a status code in the range
[400, 600).
Its status, message, and data are part of the response, so only put
information in them that is safe to show to the user.
Examples
import { HttpError } from "wasp/errors";
export const getTask = async ({ id }, context) => {
if (!context.user) {
throw new HttpError(401, "You must be logged in.");
}
// ...
};
Sending extra information and response headers:
import { HttpError } from "wasp/errors";
throw new HttpError(429, "Too many requests", {
data: { limitPerMinute: 60 },
headers: { "Retry-After": "60" },
});
Reading it on the client:
import { createTask } from "wasp/client/operations";
import { HttpError } from "wasp/errors";
try {
await createTask({ description: "Buy milk" });
} catch (error: unknown) {
if (error instanceof HttpError && error.status === 429) {
const retryAfter = error.response.headers.get("Retry-After");
}
}
See
Extends
Error
Type Parameters
Data
Data extends HttpErrorData = HttpErrorData
The type of the error's data. It defaults to
HttpErrorData, an object with any fields.
Constructors
Constructor
new HttpError<
Data>(status,message?,options?):HttpError<Data>
Creates the error together with its response.
Parameters
status
number
The HTTP status code. It must be an integer in the range
[400, 600), otherwise the constructor throws an Error.
message?
string
The error message.
options?
HttpErrorOptions<Data>
The data, the headers for the response, and the standard
Error options (e.g. cause).
Returns
HttpError<Data>
Overrides
Error.constructor
Constructor
new HttpError<
Data>(response,message?,options?):HttpError<Data>
Creates the error for an existing response, e.g. one a client received.
Parameters
response
Response
The response the error stands for. Its status must be in
the range [400, 600), otherwise the constructor throws an Error.
message?
string
The error message.
options?
Omit<HttpErrorOptions<Data>, "headers">
The data and the standard Error options (e.g. cause).
Returns
HttpError<Data>
Example
import { HttpError } from "wasp/errors";
const response = await fetch("https://api.example.com/tasks");
if (!response.ok) {
throw new HttpError(response, "Could not load the tasks.");
}
Overrides
Error.constructor
Properties
data
readonlydata:Data|undefined
Extra information about the error, e.g. which form field is invalid.
It is undefined if the error was created without it.
response
readonlyresponse:Response
The HTTP response the error stands for. Read the status code and the headers from it.
Don't read its body, since it may already be consumed. Use message and
data instead.
Accessors
status
Get Signature
get status():
number
The HTTP status code of the response, an integer in the range
[400, 600).
Returns
number
statusCode
Get Signature
get statusCode():
number
Deprecated
Use status instead.
Returns
number
Interfaces
HttpErrorOptions
The options of an HttpError. It takes the standard Error options
(e.g. cause), the data, and the headers of the error's response.
Example
import { HttpError } from "wasp/errors";
try {
await chargeCard(card);
} catch (error: unknown) {
throw new HttpError(503, "Payments are unavailable", {
headers: { "Retry-After": "60" },
cause: error,
});
}
Extends
ErrorOptions
Type Parameters
Data
Data extends HttpErrorData = HttpErrorData
The type of data.
Properties
data?
optionaldata?:Data
An object with any extra information about the error.
headers?
optionalheaders?:HeadersInit
Headers for the error response, e.g. Retry-After. To repeat a header,
like Set-Cookie, pass a list of name and value pairs.
Example
const headers: HeadersInit = [
["Set-Cookie", "a=1"],
["Set-Cookie", "b=2"],
];
Type Aliases
HttpErrorData
HttpErrorData =
object
The extra information an HttpError carries in its data. It is an
object with any fields you choose.
Index Signature
[fieldName: string]: unknown
Example
const data: HttpErrorData = { code: "TASK_LIMIT_REACHED", limit: 10 };