Skip to content

Commit

Permalink
Solves #3850
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Oct 20, 2016
1 parent 284c660 commit cb74c3c
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions lib/router/bindDefaultHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,54 @@ module.exports = function(sails) {
*/
500: function(err, req, res) {

// console.log('* * * FIRED FINAL HANDLER (500) * * *');
// console.log('* * * FIRED DEFAULT HANDLER (500) * * *');
// console.log('args:',arguments);
// console.log('* * * </FIRED_FINAL_HANDLER_500> * * *');
// console.log('* * * </FIRED_DEFAULT_HANDLER_500> * * *');
// console.log();

// First, check for special built-in errors from Express.
// We don't necessarily want to treat any error that is thrown with
// a `status` property of 400 as if it were intentional. So we also check
// the error message. In Express 5, hopefully this can be improved a bit
// further.
var msgMatches = err.message.match(/^Failed to decode param \'([^']+)\'/);
if (err.status === 400 && msgMatches) {
sails.log.verbose('Bad request: Could not decode the requested URL ('+req.path+')');
// Note for future: The problematic URL section is: `msgMatches[1]`
return res.send(400, 'Bad request: Could not decode requested URL.');
}

// Next, try to use `res.negotiate()`, if it exists and is valid.
try {
// Use error handler if it exists

if (typeof res.negotiate === 'function') {
// console.log('res.negotiate()');
return res.negotiate(err);
}
} catch (e) {}
}//>-

} catch (e) { /* ignore any unexpected error encountered when attempting to respond w/ res.negotiate(). */ }

// Catch-all:
// Log a message and try to use `res.send` to respond.
try {

// Catch-all:
// Log a message and try to use `res.send` to respond.
sails.log.error('Server Error:');
sails.log.error(err);
// console.log('catch-all.. log an error and send a 500');
if (process.env.NODE_ENV === 'production') { return res.send(500); }
return res.send(500, err);
}

// Serious error occurred-- unable to send response.
//
// Note that in the future, we could also emit an `abort` message on the request object
// in this case-- then if an attached server is managing this request, it could monitor
// for `abort` events and manage its private resources (e.g. TCP sockets) accordingly.
// However, such contingencies should really handled by the underlying HTTP hook, so
// this might not actually make sense.
catch (errorSendingResponse) {
// console.log('serious error occurred');
} catch (errorSendingResponse) {

// Serious error occurred-- unable to send response.
//
// Note that in the future, we could also emit an `abort` message on the request object
// in this case-- then if an attached server is managing this request, it could monitor
// for `abort` events and manage its private resources (e.g. TCP sockets) accordingly.
// However, such contingencies should really handled by the underlying HTTP hook, so
// this might not actually make sense.
sails.log.error('But no response could be sent because another error occurred:');
sails.log.error(errorSendingResponse);
}

}//</catch>
},


Expand Down

0 comments on commit cb74c3c

Please sign in to comment.