spee.ch/helpers/libraries/errorHandlers.js

34 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-06-20 04:34:34 +02:00
const logger = require('winston');
2017-06-30 07:26:29 +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) {
2017-07-06 03:26:33 +02:00
logger.error('Request Error >>', error);
if (error.response) {
postToStats(action, originalUrl, ip, null, null, null, null, null, error.response.data.error.messsage);
res.status(error.response.status).send(error.response.data.error.message);
} else if (error.code === 'ECONNREFUSED') {
postToStats(action, originalUrl, ip, null, null, null, null, null, 'Connection refused. The daemon may not be running.');
2017-06-27 02:16:46 +02:00
res.status(503).send('Connection refused. The daemon may not be running.');
2017-07-06 03:26:33 +02:00
} else if (error.message) {
postToStats(action, originalUrl, ip, null, null, null, null, null, error);
res.status(400).send(error.message);
2017-07-06 03:26:33 +02:00
} else {
postToStats(action, originalUrl, ip, null, null, null, null, null, error);
2017-07-06 03:26:33 +02:00
res.status(400).send(error);
}
},
handlePublishError (error) {
if (error.code === 'ECONNREFUSED') {
2017-06-28 06:32:05 +02:00
logger.error('Publish Error:', 'Connection refused. The daemon may not be running.');
return 'Connection refused. The daemon may not be running.';
} else if (error.response.data.error) {
2017-06-28 06:32:05 +02:00
logger.error('Publish Error:', error.response.data.error);
return error.response.data.error.message;
} else {
2017-07-04 17:55:40 +02:00
logger.error('Unhandled Publish Error:', error.message);
return error;
}
},
};