wildduck/lib/errors.js

67 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-09-18 17:11:09 +08:00
/* eslint global-require: 0, no-console: 0 */
2017-08-29 15:36:45 +08:00
'use strict';
const config = require('wild-config');
let bugsnag;
if (config.bugsnagCode) {
bugsnag = require('bugsnag');
bugsnag.register(config.bugsnagCode);
}
module.exports.notify = (...args) => {
if (bugsnag) {
bugsnag.notify(...args);
2017-09-18 17:11:09 +08:00
} else {
console.error(...args);
2017-08-29 15:36:45 +08:00
}
};
2017-10-02 19:42:39 +08:00
module.exports.notifyConnection = (connection, ...args) => {
2017-10-08 03:57:38 +08:00
let err = args[0];
2017-10-02 19:42:39 +08:00
let metaData = args[1] || {};
if (connection) {
if (connection.selected) {
metaData.selected = connection.selected.mailbox;
}
2017-10-02 19:42:39 +08:00
if (connection.session.user) {
metaData.userId = connection.session.user.id.toString();
}
2017-10-02 19:42:39 +08:00
metaData.remoteAddress = connection.session.remoteAddress;
metaData.isUTF8Enabled = !!connection.acceptUTF8Enabled;
}
2017-10-02 19:42:39 +08:00
2017-10-08 03:57:38 +08:00
Object.keys(err.meta || {}).forEach(key => {
metaData[key] = err.meta[key];
});
2017-10-02 19:42:39 +08:00
args[1] = metaData;
if (bugsnag) {
bugsnag.notify(...args);
} else {
console.error(...args);
}
};
module.exports.intercept = (...args) => {
if (bugsnag) {
return bugsnag.intercept(...args);
}
let cb;
if (args.length) {
cb = args[args.length - 1];
if (typeof cb === 'function') {
args[args.length - 1] = function(...rArgs) {
if (rArgs.length > 1 && rArgs[0]) {
console.error(rArgs[0]);
}
return cb(...rArgs);
};
}
}
};