RequestErrorclass
new RequestError(message?: string, { code = 400, ...options }: RequestErrorOptions = {})| Param | Type | |
|---|---|---|
message | string | Optional human-readable description of why the request was rejected. |
options | RequestErrorOptions | Optional options — code sets the HTTP status (defaults to 400); caller and contextual fields are applied via setBaseErrorOptions(). Defaults to {} |
| Return | |
|---|---|
RequestError | Throw when a request isn't well-formed or is unacceptable in some way. |
| Property | Type | |
|---|---|---|
.code | number | HTTP status code for this error in the range 400-499, defaults to 400. required readonly |
Throw when a request isn't well-formed or is unacceptable in some way.
- Carries an HTTP-style
codein the400-499client-error range (defaults to400). - Base class for the specific request errors in this file (
UnauthorizedError,NotFoundError, etc.).
Thrown when an incoming request is malformed or unacceptable — the HTTP 4xx range. Its .code property defaults to 400.
RequestError has named subclasses for common HTTP status codes, each setting its own .code: UnauthorizedError (401), ForbiddenError (403), NotFoundError (404), MethodNotAllowedError (405), and UnprocessableError (422). Throw the most specific class that fits.
Usage
import { RequestError, NotFoundError } from "shelving/error";
function loadDocument(id: string): Document {
if (!isValidId(id)) throw new RequestError("Malformed document ID", { received: id });
const doc = documents.get(id);
if (!doc) throw new NotFoundError("Document not found", { received: id }); // .code === 404
return doc;
}See shelving/error for shared behaviour — attaching context fields, caller trimming, and catching by type.
Examples
throw new RequestError("Bad request", { code: 400 });