http-server-request-handlers-error-logger

0.1.7 • Public • Published

http-server-request-handlers-error-logger

NPM version Build Status Coverage Status NSP Status

an http.Server request handler for express that handles IncomingMessage errors

table of contents

notes

this middleware should be added after all “non-error” middleware in the application.

it expects the following:

  • req is an object
  • req.headers exists
  • req.connection exists
  • res is an object
  • res.get is a function
  • res.send is a function

it implements the following:

  • creates an error object containing the following properties
{
  error: {
    body: req.body || null,
    code: err.code || null,
    date: new Date(),
    errorCode: err.errorCode || null,
    headers: req.headers || null,
    message: err.toString() || null,
    method: req.method || null,
    originalUrl: req.originalUrl || null,
    remoteAddress: req.headers[ 'x-forwarded-for' ] || req.headers[ 'x-real-ip' ] || req.connection.remoteAddress || null,
    session: req.session || null,
    status: err.status || null,
    statusCode: err.statusCode || 500
  }
}
  • adds the error statusCode to res as res.statusCode
  • logs the error object to console.error
  • logs the err.stack to console.error if the error.statusCode is not equal to 404
  • creates a message format based on the current request content-type and server environment
    • when the response content-type is application/json
      error: {
        code: err.code || null,
        errorCode: err.errorCode || null,
        message: err.toString(),
        status: err.status || null,
        statusCode: err.statusCode || 500
      }
    • when NODE_ENV is set to development and content-type is not application/json
      • the err.stack
    • otherwise the err.message

installation

npm install http-server-request-handlers-error-logger

api

/*
 * @param {Error} err
 * @param {number} err.status
 * @param {number} err.statusCode
 *
 * @param {IncomingMessage} req
 *
 * @param {ServerResponse} res
 * @param {Function} res.send
 *
 * @returns {undefined}
 */
function errorLogger( err, req, res )

use

var express = require( 'express' );
var app = express();
var errorLogger = require( 'http-server-request-handlers-error-logger' );

// ... middleware declarations
// ... route declarations

app.use( errorLogger );

license

MIT License

Package Sidebar

Install

npm i http-server-request-handlers-error-logger

Weekly Downloads

0

Version

0.1.7

License

MIT

Unpacked Size

41.3 kB

Total Files

25

Last publish

Collaborators

  • dentous