Skip to main content
Module

std/http/http_errors.ts

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
Deprecated

(will be removed in 0.207.0) Use custom error classes and http/status.ts instead.

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.

import * as mod from "https://deno.land/std@0.205.0/http/http_errors.ts";

Examples

Example 1

import { errors, isHttpError } from "https://deno.land/std@0.205.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.205.0/http/http_errors.ts";
import { Status } from "https://deno.land/std@0.205.0/http/status.ts";

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

Classes

c
HttpError
deprecated

Variables

v
errors
deprecated