http_errors

tag http_errors-ci HitCount

Create HTTP Error for Deno, inspired by http_errors.

API

import { createError, HttpError, IError, Props } from "https://deno.land/x/http_errors/mod.ts";

createError(status: number, message?: string, props?: Props): HttpError

createError(err: Error, props?: Props): IError

createError(status: number, props: Props): HttpError

Create a new error object with the given status code, message and custom properties. The error object inherits from HttpError.

const err = createError(404, 'Not found LoL!')
  • name: NotFoundError - Error name.
  • status: 404 - the status code as a number.
  • message - the message of the error, default to Deno's status code text.
  • expose: true - can be used to signal if message should be sent to the client, when status >= 400 && status < 500 it is true, otherwise false. Can be overwritten by Props.
  • properties - custom properties to attach to the error object.

HttpError

HttpError is a abstract class, can't be used to create instance.

new HttpError(404) // Error! Cannot create an instance of an abstract class.
createError(500) instanceof HttpError // true
createError(500) instanceof Error // true

Examples

Simple Http Error handle

This simple example shows how to use http_errors to return a different respond body based on different HTTP Error.

import {
  serve,
  Response,
} from "https://deno.land/std/http/server.ts";
import { createError, HttpError } from "https://deno.land/x/http_errors/mod.ts";

const server = serve("127.0.0.1:3000");
console.log("Server listening on: 3000");

for await (const req of server) {
  const res: Response = {
    body: "Hello!",
    headers: new Headers(),
  };
  try {
    if (req.url === "/4xx") {
      throw createError(403);
    } else if (req.url === "/5xx") {
      throw Error("DB error!");
    }
  } catch (e) {
    if (e instanceof HttpError && e.expose) {
      res.body = JSON.stringify(e.toJSON());
      res.status = e.status;
      console.warn(e);
    } else {
      const err = createError(500);
      res.body = JSON.stringify(err.toJSON());
      res.status = err.status;
      console.error(e);
    }
    res.headers?.set("Content-Type", "applicatoin/json");
  } finally {
    req.respond(res).catch(() => {});
  }
}

You can test this out with the cURL program:

curl -I -H'Accept: text/html' http://localhost:3000/4xx
curl -I -H'Accept: text/html' http://localhost:3000/5xx

License

MIT