spee.ch/helpers/errorHandlers.js

47 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-06-20 04:34:34 +02:00
const logger = require('winston');
2017-08-03 02:13:02 +02:00
const { postToStats } = require('../controllers/statsController.js');
2017-06-20 04:34:34 +02:00
2017-06-03 09:41:02 +02:00
module.exports = {
2017-06-28 07:41:48 +02:00
handleRequestError (action, originalUrl, ip, error, res) {
logger.error(`Request Error: ${originalUrl}`, module.exports.useObjectPropertiesIfNoKeys(error));
2017-09-14 19:05:39 +02:00
postToStats(action, originalUrl, ip, null, null, error);
if (error.response) {
res.status(error.response.status).render('requestError', {message: error.response.data.error.message, status: error.response.status});
} else if (error.code === 'ECONNREFUSED') {
res.status(503).render('requestError', {message: 'Connection refused. The daemon may not be running.', status: 503});
2017-07-06 03:26:33 +02:00
} else if (error.message) {
res.status(500).render('requestError', {message: error.message, status: 500});
2017-07-06 03:26:33 +02:00
} else {
res.status(400).render('requestError', {message: error, status: 400});
}
},
handlePublishError (error) {
logger.error('Publish Error:', module.exports.useObjectPropertiesIfNoKeys(error));
if (error.code === 'ECONNREFUSED') {
return 'Connection refused. The daemon may not be running.';
} else if (error.response) {
if (error.response.data) {
if (error.response.data.message) {
return error.response.data.message;
} else if (error.response.data.error) {
return error.response.data.error.message;
}
return error.response.data;
}
return error.response;
} else {
return error;
}
},
2017-10-03 19:05:09 +02:00
useObjectPropertiesIfNoKeys (err) {
if (Object.keys(err).length === 0) {
let newErrorObject = {};
Object.getOwnPropertyNames(err).forEach((key) => {
newErrorObject[key] = err[key];
});
return newErrorObject;
}
return err;
},
};