2
votes

I have pasted a callback below that is in my waterline model

In order to give the client a pretty message I'm attempting to modify the status of the error object so that res.negotiate(err) will respond with badRequest. But my error.status = 400 seems to be ignored and when its passed to res.negotiate I still get 500 error returned (server error instead of bad request).

I'm working off of these docs

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

Thoughts, bug?

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);
}

Also, even when I get a Server Error returned the message. In this case "This shift has people scheduled ..." can not be found on the error object that is returned? I'm using the unmodified response pages and I don't know why its being stripped out?

This is the error object being returned to the client.

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

1 Answers

0
votes

Looking at the source of the negotiate response, it appears that a 400 response should be returned:

'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);

I would suggest setting a break point within the negotiate response to verify it being called from the beforeDestroy model lifecycle callback.