-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #313 from scottnath/hotfix/error-route
errors not caught with custom err handler
- Loading branch information
Showing
8 changed files
with
270 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* @fileoverview Error routes | ||
* | ||
* @author Scott Nath | ||
* | ||
*/ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
|
||
/** | ||
* Route for a 404 error | ||
* @param {object} req - HTTP Request | ||
* @param {object} res - HTTP Response | ||
* | ||
* @returns {promise} promise containing the route | ||
*/ | ||
const missing = (req, res) => { | ||
return new Promise((resolve) => { | ||
const message = _.get(req.session, '404.message', `Not Found: ${req.url}`); | ||
const safe = _.get(req.session, '404.safe'); | ||
|
||
_.unset(req.session, '404'); | ||
|
||
res.status(404); | ||
res.render('404', { | ||
message, | ||
safe, | ||
}); | ||
resolve(true); | ||
}); | ||
}; | ||
|
||
/** | ||
* Route for errors | ||
* | ||
* @param {string|object} err - error object or error message string | ||
* @param {object} req - HTTP Request | ||
* @param {object} res - HTTP Response | ||
* @param {object} next - Express callback | ||
* @param {object} app Express application | ||
* | ||
* @returns {promise} promise containing the route | ||
*/ | ||
const errors = (err, req, res, next, app) => { | ||
return new Promise((resolve) => { | ||
let error = { | ||
message: 'route error', | ||
status: '', | ||
safe: '/', | ||
}; | ||
|
||
if (typeof err === 'object') { | ||
error = _.cloneDeep(err); | ||
} | ||
else if (typeof err === 'string' && err !== '') { | ||
error.message = err; | ||
} | ||
|
||
error.error = {}; | ||
|
||
// If in development, will print stacktrace | ||
if (app.get('env') === 'development') { | ||
error.error = err; | ||
} | ||
|
||
res.status(error.status || 500); | ||
|
||
if (error.status === 404) { | ||
res.render('404', error); | ||
} | ||
else { | ||
res.render('error', error); | ||
} | ||
|
||
resolve(true); | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
missing, | ||
errors, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.