3.1.0 • Published 4 years ago

allanchau-http-error v3.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

node-http-error

npm Report coverage Test

Simple Express HTTP error response middleware.

Features

  • Simple API error responses.

Installation

This package is available on NPM:

$ yarn add allanchau-http-error

Usage

A simple example:

// App.
const app = require("express");
const httpError = require("allanchau-http-error");

app.use(httpError());

app.get("/stringexample", (req, res, next) => {
  return res.error("Oops something went wrong.");
});

// Returns
// {
//   "code": "400",
//   "message": "Oops something went wrong.",
// }

app.get("/errorobjectexample", (req, res, next) => {
  return res.error(new Error("Oops something went wrong."));
});

// Returns the same thing
// {
//   "code": "400",
//   "message": "Oops something went wrong.",
// }

app.get("/customerrorexample", (req, res, next) => {
  return res.error({
    code: "ERRBADREQUEST",
    message: "Oops something went wrong.",
  });
});

// Returns
// {
//   "code": "ERRBADREQUEST",
//   "message": "Oops something went wrong.",
// }

You can also provide some defaults for the route.

app.use(
  httpError({
    defaultCode: "ERRNOTAUTHORIZED",
    defaultMessage: "The user is not authorized.",
    defaultStatusCode: 401,
  })
);

app.get("/stringexample", (req, res, next) => {
  return res.error();
});

// Returns
// {
//   "code": "ERRNOTAUTHORIZED",
//   "message": "The user is not authorized.",
// }
3.1.0

4 years ago

3.0.0

5 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago