mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2024-12-14 00:16:21 +00:00
e2143b4d80
ClientError will default to 400 HTTP error code. ServerError will default to 500 HTTP error code. Following the previous commit, these for now are only being used in albumsController. More will soon follow. Additionally fixed existing album names can sometimes be re-used when editing an album.
32 lines
869 B
JavaScript
32 lines
869 B
JavaScript
const ClientError = require('./../utils/ClientError')
|
|
const ServerError = require('./../utils/ServerError')
|
|
const logger = require('./../../logger')
|
|
|
|
module.exports = (error, req, res, next) => {
|
|
if (!res) {
|
|
return logger.error(new Error('Missing "res" object.'))
|
|
}
|
|
|
|
// Error messages that can be returned to users
|
|
const isClientError = error instanceof ClientError
|
|
const isServerError = error instanceof ServerError
|
|
|
|
const logStack = (!isClientError && !isServerError) ||
|
|
(isServerError && error.logStack)
|
|
if (logStack) {
|
|
logger.error(error)
|
|
}
|
|
|
|
const statusCode = (isClientError || isServerError)
|
|
? error.statusCode
|
|
: 500
|
|
|
|
const description = (isClientError || isServerError)
|
|
? error.message
|
|
: 'An unexpected error occurred. Try again?'
|
|
|
|
return res
|
|
.status(statusCode)
|
|
.json({ success: false, description })
|
|
}
|