Usage

import * as mod from "https://deno.land/std@0.152.0/http/mod.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

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;
  }
}
import { createHttpError } from "https://deno.land/std@$STD_VERSION/http/http_errors.ts";
import { Status } from "https://deno.land/std@$STD_VERSION/http/http_status.ts";

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

§Classes

HttpError

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

Server

Used to construct an HTTP server.

§Enums

Status

Standard HTTP status codes.

§Variables

errors

A map of HttpErrors that are unique instances for each HTTP error status code.

STATUS_TEXT

A record of all the status codes text.

§Functions

accepts

Returns an array of media types accepted by the request, in order of preference. If there are no media types supplied in the request, then any media type selector will be returned.

acceptsEncodings

Returns an array of content encodings accepted by the request, in order of preference. If there are no encoding supplied in the request, then ["*"] is returned, implying any encoding is accepted.

acceptsLanguages

Returns an array of languages accepted by the request, in order of preference. If there are no languages supplied in the request, then ["*"] is returned, imply any language is accepted.

createHttpError

Create an instance of an HttpError based on the status code provided.

deleteCookie

Set the cookie header with empty value in the headers to delete it

getCookies

Parse cookies of a header

isClientErrorStatus

A type guard that determines if the status code is a client error.

isErrorStatus

A type guard that determines if the status code is an error.

isHttpError

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

isInformationalStatus

A type guard that determines if the status code is informational.

isRedirectStatus

A type guard that determines if the status code is a redirection.

isServerErrorStatus

A type guard that determines if the status code is a server error.

isSuccessfulStatus

A type guard that determines if the status code is successful.

listenAndServe deprecated
listenAndServeTls deprecated
serve

Serves HTTP requests with the given handler.

serveListener

Constructs a server, accepts incoming connections on the given listener, and handles requests on these connections with the given handler.

serveTls

Serves HTTPS requests with the given handler.

setCookie

Set the cookie header properly in the headers

§Interfaces

ConnInfo

Information about the connection a request arrived on.

Cookie
HttpErrorOptions
ServeInit

Additional serve options.

ServerInit

Options for running an HTTP server.

ServeTlsInit

§Type Aliases

ClientErrorStatus

An HTTP status that is a client error (4XX).

ErrorStatus

An HTTP status that is an error (4XX and 5XX).

ErrorStatusKeys
Handler

A handler for HTTP requests. Consumes a request and connection information and returns a response.

InformationalStatus

An HTTP status that is a informational (1XX).

RedirectStatus

An HTTP status that is a redirect (3XX).

Request
ServerErrorStatus

An HTTP status that is a server error (5XX).

SuccessfulStatus

An HTTP status that is a success (2XX).