[local-sync] Don't crash app when reporting error and id not available

Summary:
Our sentry reporter tries to fetch the nylas identity from the database,
and access properties on it. However, if you are in a state where there
is no identity available (like having logged out, or just starting the
app), and encoutnered an error that would be reported to sentry, we
would throw an error while reporting and that would crash the app

Also, fix lint errors and some really janky code

This fixes T7810

Test Plan: manual

Reviewers: halla, spang, evan

Reviewed By: spang, evan

Maniphest Tasks: T7810

Differential Revision: https://phab.nylas.com/D3867
This commit is contained in:
Juan Tejada 2017-02-09 09:27:26 -08:00
parent e6afea45a0
commit 600be97324

View file

@ -1,60 +1,52 @@
/* eslint global-require: 0 */
const getMac = require('getmac').getMac
const crypto = require('crypto')
const Raven = require('raven');
//
// NOTE: This file is manually copied over from the edgehill repo into N1. You
// must manually update both files. We can't use a sym-link because require
// paths don't work properly.
//
const Raven = require('raven');
let deviceHash = "Unknown Device Hash"
let app;
module.exports = (function (...args) {
function ErrorReporter(modes) {
class ErrorReporter {
constructor(modes) {
this.reportError = this.reportError.bind(this)
this.onDidLogAPIError = this.onDidLogAPIError.bind(this);
this.inSpecMode = modes.inSpecMode
this.inDevMode = modes.inDevMode
this.resourcePath = modes.resourcePath
this.deviceHash = "Unknown Device Hash"
if (!this.inSpecMode) {
try {
getMac((err, macAddress) => {
if (!err && macAddress) {
deviceHash = crypto.createHash('md5').update(macAddress).digest('hex')
this.deviceHash = crypto.createHash('md5').update(macAddress).digest('hex')
}
this._setupSentry(deviceHash);
this._setupSentry();
})
} catch (err) {
console.error(err);
this._setupSentry(deviceHash);
this._setupSentry();
}
}
const bind = function (fn, me) { return function () { return fn.apply(me, ...args); }; };
this.onDidLogAPIError = bind(this.onDidLogAPIError, this);
}
ErrorReporter.prototype.onDidLogAPIError = function (error, statusCode, message) {
onDidLogAPIError(error, statusCode, message) { // eslint-disable-line
}
ErrorReporter.prototype._setupSentry = function (deviceHash) {
// Initialize the Sentry connector
const sentryDSN = "https://0796ad36648a40a094128d6e0287eda4:0c329e562cc74e06a48488772dd0f578@sentry.io/134984"
Raven.disableConsoleAlerts();
Raven.config(sentryDSN, {
name: deviceHash,
autoBreadcrumbs: true,
}).install();
Raven.on('error', function (e) {
console.log(e.reason);
console.log(e.statusCode);
return console.log(e.response);
});
getVersion() {
if (process.type === 'renderer') {
return NylasEnv.getVersion();
}
return require('electron').app.getVersion()
}
ErrorReporter.prototype.reportError = function (err, extra) {
reportError(err, extra) {
if (this.inSpecMode || this.inDevMode) { return }
// It's possible for there to be more than 1 sentry capture object.
@ -71,28 +63,40 @@ module.exports = (function (...args) {
const errData = {}
if (typeof app !== 'undefined' && app && app.databaseReader) {
const fullIdent = app.databaseReader.getJSONBlob("NylasID")
errData.user = {
id: fullIdent.id,
email: fullIdent.email,
name: fullIdent.firstname + " " + fullIdent.lastname,
// We may not have an identity available yet
if (fullIdent) {
errData.user = {
id: fullIdent.id,
email: fullIdent.email,
name: `${fullIdent.firstname} ${fullIdent.lastname}`,
}
}
}
for (let i = 0; i < captureObjects.length; i++) {
Raven.captureException(err, Object.assign(errData, captureObjects[i]))
}
};
}
ErrorReporter.prototype.getVersion = function () {
if (process.type === 'renderer') {
return NylasEnv.getVersion();
} else {
return require('electron').app.getVersion()
}
return null;
};
_setupSentry() {
// Initialize the Sentry connector
const sentryDSN = "https://0796ad36648a40a094128d6e0287eda4:0c329e562cc74e06a48488772dd0f578@sentry.io/134984"
ErrorReporter.prototype._prepareSentryCaptureObjects = function (error, extra) {
Raven.disableConsoleAlerts();
Raven.config(sentryDSN, {
name: this.deviceHash,
autoBreadcrumbs: true,
release: this.getVersion(),
}).install();
Raven.on('error', (e) => {
console.log(e.reason);
console.log(e.statusCode);
return console.log(e.response);
});
}
_prepareSentryCaptureObjects(error, extra) {
// Never send user auth tokens
if (error.requestOptions && error.requestOptions.auth) {
delete error.requestOptions.auth;
@ -125,6 +129,6 @@ module.exports = (function (...args) {
},
}]
}
}
return ErrorReporter;
})();
module.exports = ErrorReporter;