🌐 Nodejs.cn

错误处理器中间件

仅用于开发的错误处理中间件。

🌐 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.

当向 Express 提供一个对象作为错误时,该模块将尽可能多地显示有关此对象的信息,并通过在 HTML、JSON 和纯文本之间进行内容协商来进行响应。

🌐 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.

  • 当对象是标准的 Error 对象时,stack 属性提供的字符串将在 HTML/文本响应中返回。
  • 当对象是非 Error 对象时,HTML/文本响应中将返回 util.inspect 的结果。
  • 对于 JSON 响应,结果将是一个包含响应中对象所有可枚举属性的对象。

安装

🌐 Install

这是一个可以通过npm注册表获取的Node.js模块。安装可以使用npm install命令完成:

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

Terminal window
npm install errorhandler

Note

errorhandler 不包含其自身的 TypeScript 类型定义。如果你使用 TypeScript,还需要作为开发依赖从 DefinitelyTyped 安装社区维护的类型:

Terminal window
npm install --save-dev @types/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

提供一个函数,可使用错误和错误的字符串表示形式进行调用。可用于将错误写入任何所需的位置,或设置为 false 以仅在响应中返回错误。调用方式为 log(err, str, req, res),其中 errError 对象,str 是错误的字符串表示形式,req 是请求对象,res 是响应对象(注意,此函数在响应写入之后调用)。

🌐 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).

此选项的默认值是 true,除非 process.env.NODE_ENV === 'test'

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

可能的值:

🌐 Possible values:

  • true:使用 console.error(str) 记录错误。
  • false:只在响应中返回错误。
  • 一个函数:将错误传递给函数进行处理。

例子

🌐 Examples

简单例子

🌐 Simple example

仅在开发中将此中间件作为错误处理程序的基本示例,使用 connect(在此示例中也可以使用 express)。

🌐 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();
// assumes NODE_ENV is set by the user
if (process.env.NODE_ENV === 'development') {
// only use in development
app.use(errorhandler());
}

自定义输出位置

🌐 Custom output location

有时在开发过程中,你可能希望将错误输出到与 STDERR 不同的位置,例如系统通知。

🌐 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();
// assumes NODE_ENV is set by the user
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