Node.js 自定义错误处理 [英] Node.js custom error handling

查看:113
本文介绍了Node.js 自定义错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个应用程序.在该应用程序中,我定义了一些自定义错误,如下所示

I am working on an application. In that application I am defining some custom error like below

'use strict';

/**
 * Validation error.
 * The Default HTTP STATUS CODE for ValidationError is 400 (HTTP BAD REQUEST)
 *
 */

/* Globals */
var HTTP_BAD_REQUEST = 400;
var NAME = 'ValidationError';

/**
 * Constructor
 *
 * @param {String}      message       The validation error message
 */
function ValidationError(message) {
  this.name = NAME;
  this.message = message;
  this.code = HTTP_BAD_REQUEST;
}

ValidationError.prototype = new Error();

/**
 * Module exports
 */
module.exports = ValidationError;

现在我使用 require 在其他模块中导入这个模块

Now I use require to import this module in other modules like

var UnauthorizedError = require('../errors/UnauthorizedError')

在控制器中

// perform some logic
if(somecondition) {
    // call global error handler
    next(new ValidationError('id is must');
} else {
    // call next middleware
    next();
}

Enen if somecondition is false next() 正在调用错误处理程序中间件.

Enen if somecondition is false next() is calling error handler middleware.

我定义了我的错误处理程序中间件,如下所示

I defined my error handler middleware like below

var winston = require('winston');
var HTTP_INTERNAL_SERVER_ERROR = 500;
var MESSAGE = 'Internal Server Error';

/**
 * Express standard error middleware
 *
 * @param  {Error}      err     error object
 * @param  {Object}     req     express request object
 * @param  {Object}     res     express response object
 * @param  {Function}   next    express next function
 */
var middleware = function(err, req, res) {
  winston.error(err);
  res.status(err.code || HTTP_INTERNAL_SERVER_ERROR).json(err.message || MESSAGE);
};

/**
 * Module exports
 */
module.exports = function() {
  return middleware;
};

我在我的应用程序中使用 errorHandler 就像

I use errorHandler in my app like

app.use(errorHandler());

请注意,在错误处理程序中,我正在向客户端发送带有一些状态代码和错误消息的 json 数据.但是,如果我签入邮递员,则响应标头是

NOTICE that in error handler I am sending json data to client with some status code and error message. But if I check in postman the response headers are

Connection → keep-alive
Content-Length → 695
Content-Type → text/html; charset=utf-8
Date → Thu, 22 Jan 2015 14:18:13 GMT
X-Content-Type-Options → nosniff
X-Powered-By → Express

内容体是错误的堆栈跟踪我已经验证 next 有错误没有被调用并且 somecondition 是假的并且错误处理程序中间件也没有被调用.

The content body is stack trace of error I have verified that next with error is not called and somecondition is false and error Handler middleware is also not called.

推荐答案

我不想回答我自己的问题.但是为了社区的缘故,我发布了它.希望对大家有用.

I don't want to answer my own question. But for the sake of community I am posting it. I hope it will be useful to anyone.

下面的中间件声明

var middleware = function(err, req, res) {
  winston.error(err);
  res.status(err.code || HTTP_INTERNAL_SERVER_ERROR).json(err.message || MESSAGE);
};

错误中间件应该接受 4 个参数.

Error middleware should accept 4 arguments.

这篇关于Node.js 自定义错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆