Skip to Content
Drash v3 is in beta. APIs may change.
ReferenceCoreErrorsHTTPError

HTTPError

The uniform error type for HTTP failures. Throw it from resources and middleware; catch it where you turn errors into responses.

import { HTTPError, Resource, type HTTPRequest, } from "@drashland/drash/modules/http.native.js"; import { Status } from "@drashland/drash/core/http/response/Status.js"; class Users extends Resource { public paths = ["/users/:id"]; public GET(request: HTTPRequest) { if (!request.headers.get("authorization")) { throw new HTTPError(Status.Unauthorized, "Get out!"); } return new Response("Authed"); } }

Constructor

new HTTPError(status, message?)
ParameterTypeNotes
statusResponseStatusA status tuple, e.g. Status.Unauthorized
messagestringOptional. Defaults to the description for that status

Properties

PropertyTypeExample
namestring"HTTPError"
messagestring"Get out!"
status_codenumber401
status_code_descriptionstring"Unauthorized"

Notes

Where to Import It From?

The HTTP module re-exports HTTPError for convenience, which is why the examples import it alongside Application and Resource. You can import it from the core codebase instead if that suits you better — it is the same class, and Drash’s own internals throw it, so every error from the framework is uniform.

Why the name Check?

error instanceof HTTPError can be false even for a genuine Drash error — when the error crosses a module realm, or when two copies of Drash are loaded (a bundler duplicating the package, or mixed CJS/ESM resolution).

HTTPError therefore carries a literal name = "HTTPError", and checking error.name first is the reliable test. Check both, as above.

Default Errors Thrown Internally

StatusRaised byWhen
404ResourceNotFoundHandlerNo resource matched the URL
422RequestValidatorInput has no readable url or method
500Middleware.next()The wrapped resource method returned nothing
501ResourceAn HTTP method was not overridden
Last updated on