-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
error and not found handlers amendments and fix
- Loading branch information
Showing
2 changed files
with
16 additions
and
29 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 |
---|---|---|
@@ -1,29 +1,17 @@ | ||
const logger = require('../logger'); | ||
const { isDev } = require('../../utils'); | ||
|
||
// custom error handler for the app/api | ||
// only include error stack in development mode | ||
const errorHandler = | ||
({ json = false } = {}) => | ||
// eslint-disable-next-line no-unused-vars | ||
(err, req, res, next) => { | ||
const code = err.statusCode || 500; | ||
const stack = isDev ? err.stack : null; | ||
const error = isDev ? err.message : 'Sorry! Something went wrong.'; | ||
const errorData = { code, error, stack }; | ||
// custom error handler | ||
// eslint-disable-next-line no-unused-vars | ||
const errorHandler = () => (err, req, res, next) => { | ||
const code = err.statusCode || 500; | ||
const name = err.name || 'Error'; | ||
const message = err.message || 'Sorry! Something went wrong.'; | ||
const errorData = { name, code, message }; | ||
|
||
res.status(code); | ||
// logs error stack | ||
logger.error(err.stack); | ||
|
||
// logs error stack | ||
logger.error(err.stack); | ||
|
||
// giving erros in JSON format if request made via Ajax or `json` key is true | ||
// otherwise, rendering 500 template with passed params | ||
if (req.xhr || !!json) { | ||
res.json(errorData); | ||
} else { | ||
res.send('500 Internal Server Error.'); | ||
} | ||
}; | ||
res.status(code).json(errorData); | ||
}; | ||
|
||
module.exports = errorHandler; |
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