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