/** * A collection of HTTP errors and utilities. * * The export {@linkcode errors} contains an individual class that extends * {@linkcode HttpError} which makes handling HTTP errors in a structured way. * * The function {@linkcode createHttpError} provides a way to create instances * of errors in a factory pattern. * * The function {@linkcode isHttpError} is a type guard that will narrow a value * to an `HttpError` instance. * * @example * ```ts * import { errors, isHttpError } from "https://deno.land/std@$STD_VERSION/http/http_errors.ts"; * * try { * throw new errors.NotFound(); * } catch (e) { * if (isHttpError(e)) { * const response = new Response(e.message, { status: e.status }); * } else { * throw e; * } * } * ``` * * @example * ```ts * import { createHttpError } from "https://deno.land/std@$STD_VERSION/http/http_errors.ts"; * import { Status } from "https://deno.land/std@$STD_VERSION/http/status.ts"; * * try { * throw createHttpError( * Status.BadRequest, * "The request was bad.", * { expose: false } * ); * } catch (e) { * // handle errors * } * ``` * * @module */ import * as dntShim from "../../../../_dnt.shims.js"; import { type ErrorStatus } from "./status.js"; declare const ERROR_STATUS_MAP: { readonly BadRequest: 400; readonly Unauthorized: 401; readonly PaymentRequired: 402; readonly Forbidden: 403; readonly NotFound: 404; readonly MethodNotAllowed: 405; readonly NotAcceptable: 406; readonly ProxyAuthRequired: 407; readonly RequestTimeout: 408; readonly Conflict: 409; readonly Gone: 410; readonly LengthRequired: 411; readonly PreconditionFailed: 412; readonly RequestEntityTooLarge: 413; readonly RequestURITooLong: 414; readonly UnsupportedMediaType: 415; readonly RequestedRangeNotSatisfiable: 416; readonly ExpectationFailed: 417; readonly Teapot: 418; readonly MisdirectedRequest: 421; readonly UnprocessableEntity: 422; readonly Locked: 423; readonly FailedDependency: 424; readonly UpgradeRequired: 426; readonly PreconditionRequired: 428; readonly TooManyRequests: 429; readonly RequestHeaderFieldsTooLarge: 431; readonly UnavailableForLegalReasons: 451; readonly InternalServerError: 500; readonly NotImplemented: 501; readonly BadGateway: 502; readonly ServiceUnavailable: 503; readonly GatewayTimeout: 504; readonly HTTPVersionNotSupported: 505; readonly VariantAlsoNegotiates: 506; readonly InsufficientStorage: 507; readonly LoopDetected: 508; readonly NotExtended: 510; readonly NetworkAuthenticationRequired: 511; }; export type ErrorStatusKeys = keyof typeof ERROR_STATUS_MAP; export interface HttpErrorOptions extends ErrorOptions { expose?: boolean; headers?: dntShim.HeadersInit; } /** * The base class that all derivative HTTP extend, providing a `status` and an * `expose` property. */ export declare class HttpError extends Error { #private; constructor(message?: string, options?: HttpErrorOptions); /** A flag to indicate if the internals of the error, like the stack, should * be exposed to a client, or if they are "private" and should not be leaked. * By default, all client errors are `true` and all server errors are * `false`. */ get expose(): boolean; /** The optional headers object that is set on the error. */ get headers(): dntShim.Headers | undefined; /** The error status that is set on the error. */ get status(): ErrorStatus; } /** * A namespace that contains each error constructor. Each error extends * `HTTPError` and provides `.status` and `.expose` properties, where the * `.status` will be an error `Status` value and `.expose` indicates if * information, like a stack trace, should be shared in the response. * * By default, `.expose` is set to false in server errors, and true for client * errors. * * @example * ```ts * import { errors } from "https://deno.land/std@$STD_VERSION/http/http_errors.ts"; * * throw new errors.InternalServerError("Ooops!"); * ``` */ export declare const errors: Record; /** * A factory function which provides a way to create errors. It takes up to 3 * arguments, the error `Status`, an message, which defaults to the status text * and error options, which includes the `expose` property to set the `.expose` * value on the error. */ export declare function createHttpError(status?: ErrorStatus, message?: string, options?: HttpErrorOptions): HttpError; /** * A type guard that determines if the value is an HttpError or not. */ export declare function isHttpError(value: unknown): value is HttpError; export {};