【问题标题】:Modify a sailsjs/nodejs error object修改一个sailsjs/nodejs 错误对象
【发布时间】:2014-12-12 01:23:41
【问题描述】:

我在我的水线模型中粘贴了一个回调

为了给客户端一个漂亮的消息,我正在尝试修改错误对象的状态,以便 res.negotiate(err) 将响应 badRequest。但是我的 error.status = 400 似乎被忽略了,当它传递给 res.negotiate 时,我仍然返回 500 错误(服务器错误而不是错误请求)。

我正在研究这些文档

http://sailsjs.org/#/documentation/reference/res/res.negotiate.html https://docs.nodejitsu.com/articles/errors/what-is-the-error-object http://massalabs.com/dev/2013/10/17/handling-errors-in-nodejs.html

想法,错误?

beforeDestroy: function(criteria, next){
  var error = new Error('This shift has people scheduled and can not be deleted.');
  error.type = 'user';
  error.status = 400;
  return next(error);
}

此外,即使我收到服务器错误,也会返回该消息。在这种情况下,在返回的错误对象上找不到“此班次安排人员......”?我正在使用未修改的响应页面,但我不知道为什么它被剥离了?

这是返回给客户端的错误对象。

error: "E_UNKNOWN"
raw: {}
status: 500
summary: "Encountered an unexpected error"

【问题讨论】:

    标签: node.js sails.js waterline


    【解决方案1】:

    查看协商响应的来源,似乎应该返回 400 响应:

    'node_modules\sails\lib\hooks\responses\defaults\negotiate.js'
    
     try {
    
        statusCode = err.status || 500;
    
        // Set the status
        // (should be taken care of by res.* methods, but this sets a default just in case)
        res.status(statusCode);
    
      } catch (e) {}
    
      // Respond using the appropriate custom response
      if (statusCode === 403) return res.forbidden(body);
      if (statusCode === 404) return res.notFound(body);
      if (statusCode >= 400 && statusCode < 500) return res.badRequest(body);
    

    我建议在协商响应中设置一个断点,以验证它是从 beforeDestroy 模型生命周期回调中调用的。

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 2017-07-06
      • 2015-08-16
      • 1970-01-01
      • 2021-08-05
      相关资源
      最近更新 更多