Giter Club home page Giter Club logo

errorhandler's Introduction

errorhandler

NPM Version NPM Downloads Build Status Test Coverage

Development-only error handler middleware.

This middleware is only intended to be used in a development environment, as the full error stack traces and internal details of any object passed to this module will be sent back to the client when an error occurs.

When an object is provided to Express as an error, this module will display as much about this object as possible, and will do so by using content negotiation for the response between HTML, JSON, and plain text.

  • When the object is a standard Error object, the string provided by the stack property will be returned in HTML/text responses.
  • When the object is a non-Error object, the result of util.inspect will be returned in HTML/text responses.
  • For JSON responses, the result will be an object with all enumerable properties from the object in the response.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install errorhandler

API

var errorhandler = require('errorhandler')

errorhandler(options)

Create new middleware to handle errors and respond with content negotiation.

Options

Error handler accepts these properties in the options object.

log

Provide a function to be called with the error and a string representation of the error. Can be used to write the error to any desired location, or set to false to only send the error back in the response. Called as log(err, str, req, res) where err is the Error object, str is a string representation of the error, req is the request object and res is the response object (note, this function is invoked after the response has been written).

The default value for this option is true unless process.env.NODE_ENV === 'test'.

Possible values:

  • true: Log errors using console.error(str).
  • false: Only send the error back in the response.
  • A function: pass the error to a function for handling.

Examples

Simple example

Basic example of adding this middleware as the error handler only in development with connect (express also can be used in this example).

var connect = require('connect')
var errorhandler = require('errorhandler')

var app = connect()

if (process.env.NODE_ENV === 'development') {
  // only use in development
  app.use(errorhandler())
}

Custom output location

Sometimes you may want to output the errors to a different location than STDERR during development, like a system notification, for example.

var connect = require('connect')
var errorhandler = require('errorhandler')
var notifier = require('node-notifier')

var app = connect()

if (process.env.NODE_ENV === 'development') {
  // only use in development
  app.use(errorhandler({ log: errorNotification }))
}

function errorNotification (err, str, req) {
  var title = 'Error in ' + req.method + ' ' + req.url

  notifier.notify({
    title: title,
    message: str
  })
}

License

MIT

errorhandler's People

Contributors

cilindrox avatar dougwilson avatar evanhahn avatar jonathanong avatar ppitonak avatar yorkie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

errorhandler's Issues

Can't get errorhandler to work in Express 4

I'm new in this. I have this code:

    var express = require('express')
    var responseTime = require('response-time')
    var errorhandler = require('errorhandler')
    var app = express()

    // view engine
    app.set('view engine', 'jade')

    // middleware
    app.use(express.static('public'))
    app.use(responseTime())
    app.use(errorhandler({log: false}))

    app.get('/', function (req, res) {
      // res.render('index')
      fails()
    })

    app.listen(3000)

When I hit / I thought I should get the errorhandler in styled html, but it just shown like this:

screen shot 2016-02-04 at 9 06 23 am

What should I do to show the errorhandler html on the response??

Use util.inspect for logging errors

I'm trying to understand the reason of usage of String() in

    // write error to console
    if (env !== 'test') {
      console.error(err.stack || String(err))
    }

In my scenario, I use plain a lot plain objects as error objects. For instance,

someCall(param, function (err, result) {
  if (err) {
    // here error is something like {message: 'server error', status: 401};
    return next(err);
  }

  // ...
});

While String(err) will output [object Object] in console, instead of {message: 'server error', status: 401}. I thought it's done by mistake, but saw few tests explicitly verifying this scenario.

Could you please explain the rationale behind that? Why not just || err for not Error instances?

Please clarify copyright

Hi,

In index.js there is this holder:

/*!
 * errorhandler
 * Copyright(c) 2010 Sencha Inc.
 * Copyright(c) 2011 TJ Holowaychuk
 * MIT Licensed
 */

while README.md specify this one:

Copyright (c) 2014 Jonathan Ong [email protected]

can you please clarify copyright information, in case, unifying both ?

More readable output formats

If doing throw {a: 'a', b: [ { c: 'c' } ]} you get the output

    { a: 'a', b: [ { c: 'c' } ] }

It would be more readable, especially for larger objects, if it were pretty-printed instead of on one line and no indentation like above.

If doing throw Error({a: 'a', b: [ { c: 'c' } ]}), the first line of the output before the stack trace is

Error: [object Object]

Which is fine, but it would also be very useful to output, elsewhere on the page, a pretty-printed output of the object passed in to the Error object. Otherwise you would have to use a debugger or insert console.log commands in your code to get at the object.

read statusCode from err.statusCode?

Hi This is a question like the title, the loopback framework is using err.statusCode to expose the http status code. And I did know the preprocessor would address the issue.

However, I just think why not supporting statusCode from the error object, the node core is using this name, so should we use this as well?

Thanks !!!

setting status code

Google APIs are returning error messages in following format:

{
  errors: [{
       domain: 'global',
       reason: 'authError',
       message: 'Invalid Credentials',
       locationType: 'header',
       location: 'Authorization'
  }],
  code: 401,
  message: 'Invalid Credentials'
}

statusCode is here under code property not status.

Proposal: suppress stack traces in HTTP responses when running in production

LoopBack users are asking for an option to suppress stack traces when running in production - see strongloop/loopback#564, strongloop/loopback#1502 and strongloop/strong-remoting#87. So far, we have implemented this feature at our side, but think it would be nice to upstream that changes and make this feature available to all errorhandler users.

I can see two rules for deciding whether the stack should be included or not:

  • remove stack when NODE_ENV=production, keep it otherwise
  • remove stack when options.includeStack is false, keep it otherwise

Perhaps we can implement both of them?

  • if options.includeStack is not defined and NODE_ENV=production -> remove the stack
  • else if options.includeStack is false -> remove the stack
  • else keep the stack in

Thoughts?

Nothing displayed when error doesn't contain a .stack

Some errors, like those that less.js generate when it can't find an import do not contain a .stack parameter. When that happens in a text/css request this library cryptically generates no output and prints "undefined" to stderr.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.