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.当提供一个对象来表示为错误时,该模块将尽可能多地显示关于该对象的信息,并通过对HTML、JSON和纯文本之间的响应使用内容协商来实现。

Install

This is a Node.js module available through the npm registry. 这是一个Node.js模块,可通过npm注册表获得。Installation is done using the npm install command:使用npm install命令完成安装:

$ 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. 可用于将错误写入任何所需位置,或设置为false以仅在响应中发回错误。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).log(err, str, req, res)的形式调用,其中errError对象,str是错误的字符串表示,req是请求对象,res是响应对象(注意,此函数在写入响应后调用)。

The default value for this option is true unless process.env.NODE_ENV === 'test'.除非process.env.NODE_ENV === 'test',否则此选项的默认值为true

Possible values:可能值:

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).添加此中间件作为错误处理程序的基本示例仅在使用connect的开发中使用(此示例中也可以使用express)。

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.有时,您可能希望在开发期间将错误输出到与STDERR不同的位置,例如系统通知。

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