server-error-handler

1.0.2 • Public • Published

server-error-handler

js-semistandard-style

HTTP application internal server error handler.

It can be used with any HTTP application framework (Express etc) whose response object provides status(httpStatusCode) and send(payload) methods to send an 500 Internal Server Error response with the following extensions:

  • In a production environment, it sends empty payload but asynchronously executes functions passed within the options object.

  • In a non-production environment, it sends payload that contains information about the error occurred.

Creating a singleton instance per application

const app = require('express')();
const serverErrorHandlerOptions = {
  logger: function (err) { /* Log the error */ },
  notificator: function (err) { /* Send a message to an admin */ }
};
const serverErrorHandler = require('server-error-handler')(serverErrorHandlerOptions);

Usage

...
callSomeAsyncFunction(data, function (err, someReturnedValue) {
  if (err) {
    serverErrorHandler.handle(err);
    return;
  }
  /* Normal path of execution */
});
...

API

Constructor

const serverErrorHandler = require('server-error-handler')(options)

// or you can explicitly construct it

const serverErrorHandler = new require('server-error-handler')(options)

options

Type: Object

Optional. Configuration object that may have the following properties:

Property Name Type Description
logger {Function} Optional. A function that will asynchronously be called to log the error. The function will be passed the Error object.
notificator {Function} Optional. A function that will be asynchronously be called to send a notice to an administrator about the error. The function will be passed the Error object.

Methods

handle(err, res)

err

Type: Object<Error>

An Error caught somewhere in your code.

res

Type: Object

The HTTP response object provided by Node.js or a framework.

Readme

Keywords

Package Sidebar

Install

npm i server-error-handler

Weekly Downloads

0

Version

1.0.2

License

MIT

Last publish

Collaborators

  • agorokhov