Skip to content

Commit

Permalink
Improve error reporting, reduce data that being sent
Browse files Browse the repository at this point in the history
  • Loading branch information
Paxa committed Aug 30, 2021
1 parent 2453e8c commit 792a8a1
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 128 deletions.
2 changes: 1 addition & 1 deletion app/views/history_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class HistoryWindow {

newWindow.loadURL('file://' + App.root + '/views/history_window.html');

if (process.env.NW_DEBUG == "true") {
if (process.env.POSTBIRD_DEBUG == "true") {
newWindow.webContents.toggleDevTools();
}

Expand Down
2 changes: 1 addition & 1 deletion app/views/snippets.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SnippetsWindow {

newWindow.setTitle("Snippets");

if (process.env.NW_DEBUG == "true") {
if (process.env.POSTBIRD_DEBUG == "true") {
newWindow.webContents.toggleDevTools();
}

Expand Down
110 changes: 42 additions & 68 deletions lib/error_reporter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
var child_process = require('child_process');
var colors = require('colors/safe');

var initSentry = function initSentry() {
if (process.env.POSTBIRD_DEV != "true") {
var Sentry = require('@sentry/electron');
var integrationsPkg = require("@sentry/integrations");
Sentry.init({
dsn: 'https://07fa68e1ac02484e9370fc9f0b77631f:[email protected]/143647',
debug: false,
appName: 'Postbird',
integrations: function(integrations) {
integrations.push(new integrationsPkg.Dedupe());
return integrations;
},
beforeBreadcrumb(breadcrumb, hint) {
return null;
},
});
}
}

var errorReporter = function errorReporter(exception /*: PgError */, showError = true) {
setTimeout(function () {
// skip errors while developing
if (process.env.NW_DEV == "true") {
if (process.env.POSTBIRD_DEV == "true") {
logger.info("Skip error reporting");
logger.info(exception.stack);
try {
Expand All @@ -20,7 +38,8 @@ var errorReporter = function errorReporter(exception /*: PgError */, showError =
exception.message.includes("server closed the connection unexpectedly") ||
exception.message.includes("Unable to set non-blocking to true") ||
exception.message.includes("Client has encountered a connection error and is not queryable") ||
exception.message.includes("Connection terminated")
exception.message.includes("Connection terminated") ||
exception.message.includes("current transaction is aborted,")
) {
console.log("Skip error reporting");
console.error(exception.stack);
Expand All @@ -35,58 +54,12 @@ var errorReporter = function errorReporter(exception /*: PgError */, showError =
exception.client = "CUT!";
}

var Raven = require('raven');
Raven.config('https://07fa68e1ac02484e9370fc9f0b77631f:[email protected]/143647', {
logger: 'default',
allowSecretKey: true,
transport: {
send(client, message, headers, eventId, cb) {
setTimeout(() => {
try {
var net = require('electron').remote.net;
var request = net.request({
method: 'POST',
protocol: 'https:',
hostname: client.dsn.host,
path: client.dsn.path + 'api/' + client.dsn.project_id + '/store/',
headers: headers,
port: 443
});

request.on('error', error => {
console.error(error);
});

request.on('abort', error => {
console.error('abort', error);
});

request.on('response', res => {
console.log('Error reported');
});

request.end(message);
} catch (error) {
console.error(error);
}
}, 100);
}
}
}).install();

// initSentry();
var Sentry = require('@sentry/electron');
var electron = require('electron');
var electronVersion;
try {
electronVersion = electron.remote.process.versions.electron;
} catch (e) {
console.error(e.stack);
}

var extra /*: any */ = {
user: global.process.env.USER,
pwd: global.process.env.PWD,
arch: global.process.arch,
version_el: electronVersion
};

try {
Expand All @@ -104,20 +77,15 @@ var errorReporter = function errorReporter(exception /*: PgError */, showError =
console.error(error);
}

var sender = function () {
Raven.setContext({user: extra});
Raven.captureException(exception);
};

if (process.platform == "darwin") {
var exec = child_process.exec;
exec('sw_vers -productVersion', function (err, stdout) {
extra.system = stdout;
sender();
});
} else {
sender();
if (global.App.currentTab) {
extra.appTab = global.App.currentTab.name;
extra.appPane = global.App.currentTab.instance.currentTab;
}

Sentry.configureScope(scope => {
scope.setExtra('user', extra);
});
Sentry.captureException(exception);
});

if (global.logger) {
Expand All @@ -129,14 +97,20 @@ var errorReporter = function errorReporter(exception /*: PgError */, showError =
}

if (showError) {
if (exception instanceof Error) {
window.alert(exception.message + "\n" + exception.stack);
} else {
window.alert(exception);
try {
if (exception instanceof Error) {
window.alert(exception.message + "\n" + exception.stack);
} else {
window.alert(exception);
}
} catch (e) {
console.log("can not show error", e);
}
}
return false;
};

errorReporter.init = initSentry;

process.on("uncaughtException", errorReporter);
module.exports = errorReporter;
4 changes: 3 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const windowStateKeeper = require('electron-window-state');
electron.app.ApplicationStart = Date.now();
electron.app.MainFilename = process.mainModule.filename;

require('./lib/error_reporter').init();

// Report crashes to our server.
//require('crash-reporter').start();

Expand Down Expand Up @@ -91,7 +93,7 @@ app.on('ready', () => {
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/index.html');

if (process.env.NW_DEBUG == "true") {
if (process.env.POSTBIRD_DEBUG == "true") {
mainWindow.webContents.openDevTools({detach: true});
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"pg": "8.7.1",
"pg-escape": "^0.2.0",
"pug": "3.0.2",
"raven": "2.6.4",
"@sentry/electron": "2.5.2",
"@sentry/integrations": "6.11.0",
"semver": "^7.3.5",
"slash": "^3.0.0",
"sprintf-js": "1.1.2",
Expand Down
4 changes: 2 additions & 2 deletions run
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

export NW_DEV=true
export NW_DEBUG=true
export POSTBIRD_DEV=true
export POSTBIRD_DEBUG=true

node_modules/.bin/electron . $@
Loading

0 comments on commit 792a8a1

Please sign in to comment.