修改一个sailsjs / nodejs错误对象

我已经在我的水线模型下粘贴了一个callback

为了给客户端一个漂亮的消息,我试图修改错误对象的状态,以便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 /十分之二千零十三/ 17 /处理-错误合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); } 

此外,即使当我得到一个服务器错误返回的消息。 在这种情况下,返回的错误对象上找不到“此class次有人安排…”。 我正在使用未修改的响应页面,我不知道为什么被删除?

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

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

从谈判回应的来源看来,应该返回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); 

我会build议在协商响应中设置一个断点,以validation它是从beforeDestroy模型生命周期callback中调用的。