2017-06-19 19:34:34 -07:00
|
|
|
const logger = require('winston');
|
2017-08-02 17:13:02 -07:00
|
|
|
const { postToStats } = require('../controllers/statsController.js');
|
2017-06-19 19:34:34 -07:00
|
|
|
|
2017-09-14 11:34:13 -07:00
|
|
|
function useObjectPropertiesIfNoKeys (err) {
|
|
|
|
if (Object.keys(err).length === 0) {
|
|
|
|
let newErrorObject = {};
|
|
|
|
Object.getOwnPropertyNames(err).forEach((key) => {
|
|
|
|
newErrorObject[key] = err[key];
|
|
|
|
});
|
|
|
|
return newErrorObject;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-06-03 00:41:02 -07:00
|
|
|
module.exports = {
|
2017-06-27 22:41:48 -07:00
|
|
|
handleRequestError (action, originalUrl, ip, error, res) {
|
2017-09-14 11:34:13 -07:00
|
|
|
logger.error('Request Error:', useObjectPropertiesIfNoKeys(error));
|
2017-09-14 10:05:39 -07:00
|
|
|
postToStats(action, originalUrl, ip, null, null, error);
|
2017-06-30 15:31:23 -07:00
|
|
|
if (error.response) {
|
2017-06-19 18:37:35 +02:00
|
|
|
res.status(error.response.status).send(error.response.data.error.message);
|
2017-06-17 22:51:30 +02:00
|
|
|
} else if (error.code === 'ECONNREFUSED') {
|
2017-06-26 17:16:46 -07:00
|
|
|
res.status(503).send('Connection refused. The daemon may not be running.');
|
2017-07-05 18:26:33 -07:00
|
|
|
} else if (error.message) {
|
2017-07-04 00:02:47 -07:00
|
|
|
res.status(400).send(error.message);
|
2017-07-05 18:26:33 -07:00
|
|
|
} else {
|
|
|
|
res.status(400).send(error);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
handlePublishError (error) {
|
2017-09-14 11:34:13 -07:00
|
|
|
logger.error('Publish Error:', useObjectPropertiesIfNoKeys(error));
|
2017-06-17 22:51:30 +02:00
|
|
|
if (error.code === 'ECONNREFUSED') {
|
2017-06-19 18:37:35 +02:00
|
|
|
return 'Connection refused. The daemon may not be running.';
|
2017-10-05 14:48:08 -07:00
|
|
|
} 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;
|
2017-06-17 22:51:30 +02:00
|
|
|
} else {
|
2017-06-19 18:37:35 +02:00
|
|
|
return error;
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|