Skip to content

Commit

Permalink
Improve default serverError handler in builtin router. Also, I'll pro…
Browse files Browse the repository at this point in the history
…bably regret this later, but added an editorconfig file.
  • Loading branch information
mikermcneil committed Mar 25, 2014
1 parent dafd9c9 commit 51a9001
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 75 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
147 changes: 72 additions & 75 deletions lib/router/bindDefaultHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,81 +13,78 @@ var util = require('util');
module.exports = function (sails) {


return {

/**
* Default 500 handler.
* (for errors implicitly thrown in middleware/routes)
*
* @param {*} err
* @param {Request} req
* @param {Response} res
*/
500: function (err, req, res) {
try {
// Use error handler if it exists
// (actually don't, because other problems might exist w/ res.view() or req._sails missing)
// if ( typeof res.serverError === 'function' ) {
// return res.serverError(err);
// }

// Catch-all:
// Log a message and try to use `res.send` to respond.
sails.log.error('A server error occurred in a request:');
sails.log.error(err);
res.send(500, util.inspect(err));
return;
}
catch(e) {

//
// Serious error occurred-- unable to send response.
// TODO: try to kill the abort the request (in case it is still open)
//

sails.log.error('A server error was encountered in a request:');
sails.log.error(err);
sails.log.error('But no response could be sent because another error occurred:\n', e);
return;
}
},



/**
* Default 404 handler.
* (for unmatched routes)
*
* @param {Request} req
* @param {Response} res
*/
404: function (req, res) {
try {
// Use notFound handler if it exists
// (actually don't, because other problems might exist w/ res.view() or req._sails missing)
// if ( typeof res.notFound === 'function' ) {
// return res.notFound();
// }

// Catch-all:
// Log a message and try to use `res.send` to respond.
sails.log.verbose('A request did not match any routes, and no notFound handler is configured.');
res.send(404);
return;
}
catch(e) {

//
// Serious error occurred-- unable to send response.
// TODO: try to kill the abort the request (in case it is still open)
//

sails.log.error('An unmatched route was encountered in a request...');
sails.log.error('But no response could be sent because an error occurred:\n', util.inspect(e));
return;
}
}
};
return {

/**
* Default 500 handler.
* (for errors implicitly thrown in middleware/routes)
*
* @param {*} err
* @param {Request} req
* @param {Response} res
*/
500: function (err, req, res) {

try {
// Use error handler if it exists
if ( typeof res.serverError === 'function' ) {
return res.serverError(err);
}
} catch(e) {}

// Catch-all:
// Log a message and try to use `res.send` to respond.
sails.log.error('Server Error:');
var msg = typeof err === 'object' && err instanceof Error ? err.stack : util.inspect(err);
sails.log.error(msg);

try {
if (process.env.NODE_ENV === 'production') return res.send(500);
return res.send(500, msg);
}
catch (errorSendingResponse) {
sails.log.error('But no response could be sent because another error occurred:\n', errorSendingResponse);
// Serious error occurred-- unable to send response.
// TODO: try to abort the request (in case it is still open)
}
},



/**
* Default 404 handler.
* (for unmatched routes)
*
* @param {Request} req
* @param {Response} res
*/
404: function (req, res) {
try {
// Use notFound handler if it exists
// (actually don't, because other problems might exist w/ res.view() or req._sails missing)
// if ( typeof res.notFound === 'function' ) {
// return res.notFound();
// }

// Catch-all:
// Log a message and try to use `res.send` to respond.
sails.log.verbose('A request did not match any routes, and no notFound handler is configured.');
res.send(404);
return;
}
catch(e) {

//
// Serious error occurred-- unable to send response.
// TODO: try to kill the abort the request (in case it is still open)
//

sails.log.error('An unmatched route was encountered in a request...');
sails.log.error('But no response could be sent because an error occurred:\n', util.inspect(e));
return;
}
}
};

};

0 comments on commit 51a9001

Please sign in to comment.