Skip to main content
Module

std/http/http_errors.ts

Deno standard library
Go to Latest
import * as mod from "https://deno.land/std@0.190.0/http/http_errors.ts";

A collection of HTTP errors and utilities.

The export errors contains an individual class that extends HttpError which makes handling HTTP errors in a structured way.

The function createHttpError provides a way to create instances of errors in a factory pattern.

The function isHttpError is a type guard that will narrow a value to an HttpError instance.

Examples

Example 1

import { errors, isHttpError } from "https://deno.land/std@0.190.0/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 2

import { createHttpError } from "https://deno.land/std@0.190.0/http/http_errors.ts";
import { Status } from "https://deno.land/std@0.190.0/http/http_status.ts";

try {
  throw createHttpError(
    Status.BadRequest,
    "The request was bad.",
    { expose: false }
  );
} catch (e) {
  // handle errors
}

Classes

The base class that all derivative HTTP extend, providing a status and an expose property.

Variables

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.

Functions

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 incudes the expose property to set the .expose value on the error.

A type guard that determines if the value is an HttpError or not.