@acruzjr/express-http-errors
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Express HTTP Errors Handler

Build Status npm version

A simple node.js module that handle HTTP Errors for express. You can handle HTTP errors easier using object-orientation.

Install

npm install @acruzjr/express-http-errors

Usage

Initialize your express app, add your routes and make it uses ExpressErrorHandle after ALL middlewares and routes. Using ExpressErrorHandle before routes declaration will not work. Throw your exception inside express NextFunction.

var app = require("express")();
var { ExpressErrorHandler } = require("@acruzjr/express-http-errors");

app.get(
  '/test400',
  middleware400
);

// curl -s localhost:3000/test400
// Expect BadRequest Error
function middleware400(req, res, next) {
  return next(new BadRequestError('You typed something wrong. It\'s never late to fix it.'))
}

app.get(
  '/test404',
  middleware404
);

// curl -s localhost:3000/test404
// Expect NotFound Error
function middleware404(req, res, next) {
  return next(new NotFoundError('You are beyond borders'))
}

app.get(
  '/test500',
  middleware500
);

// curl -s localhost:3000/test500
// Expect InternalServerError
function middleware500(req, res, next) {
  return next(new InternalServerError('Well, this is embarrassing...'))
}

// Make the app use error handler middleware AFTER ALL middlewares and routes uses.
app.use(ExpressErrorHandler);

app.listen(3000);

API

HTTPErrorName(message, error)

message - string

error - any, error object

new BadRequestError(message: string, error: unknown)

Examples

Examples are available here

Test

npm test

Dependents (0)

Package Sidebar

Install

npm i @acruzjr/express-http-errors

Weekly Downloads

3

Version

1.0.2

License

MIT

Unpacked Size

36.3 kB

Total Files

12

Last publish

Collaborators

  • acruzjr