2017-06-20 04:34:34 +02:00
|
|
|
const logger = require('winston');
|
|
|
|
|
2017-06-03 09:41:02 +02:00
|
|
|
module.exports = {
|
2017-11-02 19:50:05 +01:00
|
|
|
returnErrorMessageAndStatus: function (error) {
|
2017-11-03 21:37:23 +01:00
|
|
|
let status, message;
|
|
|
|
// check for daemon being turned off
|
2017-06-17 22:51:30 +02:00
|
|
|
if (error.code === 'ECONNREFUSED') {
|
2017-11-02 19:50:05 +01:00
|
|
|
status = 503;
|
|
|
|
message = 'Connection refused. The daemon may not be running.';
|
2017-11-03 21:37:23 +01:00
|
|
|
// check for errors from the deamon
|
2017-10-05 23:48:08 +02:00
|
|
|
} else if (error.response) {
|
2017-11-02 19:50:05 +01:00
|
|
|
status = error.response.status || 500;
|
2017-10-05 23:48:08 +02:00
|
|
|
if (error.response.data) {
|
|
|
|
if (error.response.data.message) {
|
2017-11-02 19:50:05 +01:00
|
|
|
message = error.response.data.message;
|
2017-10-05 23:48:08 +02:00
|
|
|
} else if (error.response.data.error) {
|
2017-11-02 19:50:05 +01:00
|
|
|
message = error.response.data.error.message;
|
|
|
|
} else {
|
|
|
|
message = error.response.data;
|
2017-10-05 23:48:08 +02:00
|
|
|
}
|
2017-11-02 19:50:05 +01:00
|
|
|
} else {
|
|
|
|
message = error.response;
|
2017-10-05 23:48:08 +02:00
|
|
|
}
|
2017-12-14 21:32:20 +01:00
|
|
|
// check for thrown errors
|
2017-11-03 21:37:23 +01:00
|
|
|
} else if (error.message) {
|
|
|
|
status = 400;
|
|
|
|
message = error.message;
|
|
|
|
// fallback for everything else
|
2017-06-17 22:51:30 +02:00
|
|
|
} else {
|
2017-11-03 21:37:23 +01:00
|
|
|
status = 400;
|
2017-11-02 19:50:05 +01:00
|
|
|
message = error;
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
2017-11-02 19:50:05 +01:00
|
|
|
return [status, message];
|
|
|
|
},
|
2017-12-07 03:49:05 +01:00
|
|
|
handleRequestError: function (originalUrl, ip, error, res) {
|
2017-11-02 19:50:05 +01:00
|
|
|
logger.error(`Request Error on ${originalUrl}`, module.exports.useObjectPropertiesIfNoKeys(error));
|
2017-11-14 23:29:34 +01:00
|
|
|
const [status, message] = module.exports.returnErrorMessageAndStatus(error);
|
2017-11-02 19:50:05 +01:00
|
|
|
res
|
2017-11-13 21:12:16 +01:00
|
|
|
.status(status)
|
2017-11-14 23:29:34 +01:00
|
|
|
.render('requestError', module.exports.createErrorResponsePayload(status, message));
|
2017-11-02 19:50:05 +01:00
|
|
|
},
|
2017-12-07 03:49:05 +01:00
|
|
|
handleApiError: function (originalUrl, ip, error, res) {
|
|
|
|
logger.error(`Api Error on ${originalUrl}`, module.exports.useObjectPropertiesIfNoKeys(error));
|
2017-11-14 23:29:34 +01:00
|
|
|
const [status, message] = module.exports.returnErrorMessageAndStatus(error);
|
2017-11-02 19:50:05 +01:00
|
|
|
res
|
2017-11-13 21:12:16 +01:00
|
|
|
.status(status)
|
2017-11-14 23:29:34 +01:00
|
|
|
.json(module.exports.createErrorResponsePayload(status, message));
|
2017-10-07 03:04:20 +02:00
|
|
|
},
|
2017-11-02 19:50:05 +01:00
|
|
|
useObjectPropertiesIfNoKeys: function (err) {
|
2017-10-07 03:04:20 +02:00
|
|
|
if (Object.keys(err).length === 0) {
|
|
|
|
let newErrorObject = {};
|
|
|
|
Object.getOwnPropertyNames(err).forEach((key) => {
|
|
|
|
newErrorObject[key] = err[key];
|
|
|
|
});
|
|
|
|
return newErrorObject;
|
|
|
|
}
|
|
|
|
return err;
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2017-11-13 21:12:16 +01:00
|
|
|
createErrorResponsePayload (status, message) {
|
|
|
|
return {
|
|
|
|
status,
|
|
|
|
success: false,
|
|
|
|
message,
|
|
|
|
};
|
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|