diff --git a/src/commands/testCrash.js b/src/commands/testCrash.js index 0725988a..5675a1ee 100644 --- a/src/commands/testCrash.js +++ b/src/commands/testCrash.js @@ -8,10 +8,10 @@ import { createCommand, Command } from 'helpers/ipc' type Input = void type Result = void -const cmd: Command = createCommand('testCrash', () => - Observable.create(() => { +const cmd: Command = createCommand('testCrash', () => { + return Observable.create(() => { process.exit(1) - }), -) + }) +}) export default cmd diff --git a/src/init-sentry.js b/src/init-sentry.js deleted file mode 100644 index 663c6840..00000000 --- a/src/init-sentry.js +++ /dev/null @@ -1,7 +0,0 @@ -const { SENTRY_URL } = process.env - -if (__PROD__ && SENTRY_URL) { - // const Raven = require('raven') - // const ravenConfig = { captureUnhandledRejections: true } - // Raven.config(SENTRY_URL, ravenConfig).install() -} diff --git a/src/internals/index.js b/src/internals/index.js index 94601aaa..f2879c08 100644 --- a/src/internals/index.js +++ b/src/internals/index.js @@ -3,14 +3,18 @@ import commands from 'commands' import logger from 'logger' import uuid from 'uuid/v4' import { setImplementation } from 'api/network' +import sentry from 'sentry/node' require('../env') -require('../init-sentry') process.title = 'Internal' const defers = {} +let sentryEnabled = process.env.INITIAL_SENTRY_ENABLED || false + +sentry(() => sentryEnabled, process.env.SENTRY_USER_ID) + if (process.env.DEBUG_NETWORK) { setImplementation(networkArg => { const id = uuid() @@ -92,6 +96,9 @@ process.on('message', m => { } else { defer.reject(payload.error) } + } else if (m.type === 'sentryLogsChanged') { + const { payload } = m + sentryEnabled = payload.value } }) diff --git a/src/main/bridge.js b/src/main/bridge.js index ff9af1ab..bd118629 100644 --- a/src/main/bridge.js +++ b/src/main/bridge.js @@ -7,6 +7,8 @@ import { ipcMain, app } from 'electron' import { ipcMainListenReceiveCommands } from 'helpers/ipc' import path from 'path' import logger from 'logger' +import sentry from 'sentry/node' +import user from 'helpers/user' import setupAutoUpdater, { quitAndInstall } from './autoUpdate' @@ -17,6 +19,11 @@ const LEDGER_LIVE_SQLITE_PATH = path.resolve(app.getPath('userData'), 'sqlite') let internalProcess +let sentryEnabled = false +const userId = user().id + +sentry(() => sentryEnabled, userId) + const killInternalProcess = () => { if (internalProcess) { logger.log('killing internal process...') @@ -30,7 +37,12 @@ const forkBundlePath = path.resolve(__dirname, `${__DEV__ ? '../../' : './'}dist const bootInternalProcess = () => { logger.log('booting internal process...') internalProcess = fork(forkBundlePath, { - env: { ...process.env, LEDGER_LIVE_SQLITE_PATH }, + env: { + ...process.env, + LEDGER_LIVE_SQLITE_PATH, + INITIAL_SENTRY_ENABLED: sentryEnabled, + SENTRY_USER_ID: userId, + }, }) internalProcess.on('message', handleGlobalInternalMessage) internalProcess.on('exit', code => { @@ -102,6 +114,13 @@ ipcMain.on('executeHttpQueryPayload', (event, payload) => { p.send({ type: 'executeHttpQueryPayload', payload }) }) +ipcMain.on('sentryLogsChanged', (event, payload) => { + sentryEnabled = payload.value + const p = internalProcess + if (!p) return + p.send({ type: 'sentryLogsChanged', payload }) +}) + // TODO move this to "command" pattern ipcMain.on('updater', (event, { type, data }) => { const handler = { diff --git a/src/main/index.js b/src/main/index.js index 3670ac42..65952b72 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -4,7 +4,6 @@ process.setMaxListeners(0) require('../env') require('../globals') -// require('../init-sentry') require('./app') setImmediate(() => require('./bridge')) diff --git a/src/middlewares/sentry.js b/src/middlewares/sentry.js index 479be983..212764c6 100644 --- a/src/middlewares/sentry.js +++ b/src/middlewares/sentry.js @@ -1,19 +1,14 @@ -const Raven = require('raven-js') -require('../env') - +import { ipcRenderer } from 'electron' import { sentryLogsBooleanSelector } from 'reducers/settings' -const { SENTRY_URL } = process.env - let isSentryInstalled = false export default store => next => action => { next(action) - if (__PROD__ && SENTRY_URL) { - const state = store.getState() - const sentryLogs = sentryLogsBooleanSelector(state) - // if (sentryLogs !== isSentryInstalled) { - // - // } + const state = store.getState() + const sentryLogs = sentryLogsBooleanSelector(state) + if (sentryLogs !== isSentryInstalled) { + isSentryInstalled = sentryLogs + ipcRenderer.send('sentryLogsChanged', { value: sentryLogs }) } } diff --git a/src/renderer/index.js b/src/renderer/index.js index 5ae7edce..5164fc17 100644 --- a/src/renderer/index.js +++ b/src/renderer/index.js @@ -1,14 +1,3 @@ require('@babel/polyfill') - -const Raven = require('raven-js') - require('../env') - -const { SENTRY_URL } = process.env - -if (__PROD__ && SENTRY_URL) { - // Raven.config(SENTRY_URL, { allowSecretKey: true }).install() - // window.addEventListener('unhandledrejection', event => Raven.captureException(event.reason)) -} - require('./init') diff --git a/src/renderer/init.js b/src/renderer/init.js index 7bc6fe3d..c9e77136 100644 --- a/src/renderer/init.js +++ b/src/renderer/init.js @@ -14,8 +14,7 @@ import events from 'renderer/events' import { fetchAccounts } from 'actions/accounts' import { fetchSettings } from 'actions/settings' import { isLocked } from 'reducers/application' -import { getLanguage } from 'reducers/settings' -import { sentryLogsBooleanSelector } from 'reducers/settings' +import { getLanguage, sentryLogsBooleanSelector } from 'reducers/settings' import libcoreGetVersion from 'commands/libcoreGetVersion' import db from 'helpers/db' @@ -23,7 +22,7 @@ import dbMiddleware from 'middlewares/db' import CounterValues from 'helpers/countervalues' import hardReset from 'helpers/hardReset' -import sentry from 'renderer/sentry/browser' +import sentry from 'sentry/browser' import App from 'components/App' import 'styles/global' diff --git a/src/renderer/sentry/browser.js b/src/renderer/sentry/browser.js deleted file mode 100644 index 9ee54b29..00000000 --- a/src/renderer/sentry/browser.js +++ /dev/null @@ -1,19 +0,0 @@ -const Raven = require('raven-js') -require('../../env') -import user from 'helpers/user' - -const { SENTRY_URL } = process.env - -export default shouldSendCallback => { - Raven.config(SENTRY_URL, { - allowSecretKey: true, - release: __APP_VERSION__, - environment: __DEV__ ? 'development' : 'production', - shouldSendCallback, - }) - .setUserContext({ - ip_address: null, - id: user().id, - }) - .install() -} diff --git a/src/renderer/sentry/node.js b/src/renderer/sentry/node.js deleted file mode 100644 index e69de29b..00000000 diff --git a/src/sentry/browser.js b/src/sentry/browser.js new file mode 100644 index 00000000..ac5fc55c --- /dev/null +++ b/src/sentry/browser.js @@ -0,0 +1,9 @@ +// @flow + +import Raven from 'raven-js' +import user from 'helpers/user' +import install from './install' + +export default (shouldSendCallback: () => boolean) => { + install(Raven, shouldSendCallback, user().id) +} diff --git a/src/sentry/install.js b/src/sentry/install.js new file mode 100644 index 00000000..acfb33e0 --- /dev/null +++ b/src/sentry/install.js @@ -0,0 +1,26 @@ +// @flow + +require('../env') + +const { SENTRY_URL } = process.env + +export default (Raven: any, shouldSendCallback: () => boolean, userId: string) => { + if (!SENTRY_URL) return + let r = Raven.config(SENTRY_URL, { + captureUnhandledRejections: true, + allowSecretKey: true, + release: __APP_VERSION__, + environment: __DEV__ ? 'development' : 'production', + shouldSendCallback, + }) + const user = { + ip_address: null, + id: userId, + } + if (r.setUserContext) { + r = r.setUserContext(user) + } else if (r.setContext) { + r = r.setContext({ user }) + } + r.install() +} diff --git a/src/sentry/node.js b/src/sentry/node.js new file mode 100644 index 00000000..a617487b --- /dev/null +++ b/src/sentry/node.js @@ -0,0 +1,8 @@ +// @flow + +import Raven from 'raven' +import install from './install' + +export default (shouldSendCallback: () => boolean, userId: string) => { + install(Raven, shouldSendCallback, userId) +}