Skip to content

Commit

Permalink
Initialize Sentry after checking config file
Browse files Browse the repository at this point in the history
  • Loading branch information
sarathms committed Feb 12, 2021
1 parent 14064e1 commit 24b8738
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 49 deletions.
5 changes: 4 additions & 1 deletion main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ const { getConfig, maybeMigrate, initConfigFile } = require('./utils/config')
const { mainWindow, openAboutWindow, windowURL } = require('./windows')
const toggleWindow = require('./windows/toggle')
const { ipcBindingsForMain } = require('./ipcBindings')
const initializeSentry = require('./utils/sentry')

// Get sentry up and running (if already)
initializeSentry()

require('./utils/sentry')
require('debug-to-file')
require('electron-unhandled')()
require('electron-debug')({
Expand Down
3 changes: 3 additions & 0 deletions main/utils/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ const initConfigFile = async () => {
}
await fs.ensureFile(OONI_CONFIG_PATH)
await fs.writeJson(OONI_CONFIG_PATH, config, {spaces: ' '})

// Now that a config file is available, let's try to initialize Sentry again
require('../utils/sentry')()
}

// Utility function to set an index of an object based on dot notation.
Expand Down
2 changes: 1 addition & 1 deletion main/utils/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const debugGetAllPaths = () => ({
'binaryDirectory': getBinaryDirectory(),
'binarySuffix': getBinarySuffix(),
'homeDir': getHomeDir(),
'logFile': log.transports.file.findLogPath(),
'logFile': log.transports.file.getFile().path,
})

module.exports = {
Expand Down
34 changes: 27 additions & 7 deletions main/utils/sentry.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
/* global module */
const { init } = require('@sentry/electron')
const isDev = require('electron-is-dev')
const log = require('electron-log')

const { name } = require('../../package.json')
const { getConfig } = require('../utils/config')

const initializeSentry = async () => {
log.info('Initializing Sentry in main...')
try {
const config = await getConfig()
if (config && config.hasOwnProperty('advanced')
&& config.advanced.send_crash_reports === true
) {
init({
enabled: process.env.NODE_ENV === 'production',
dsn: 'https://[email protected]/1210892',
environment: isDev ? 'development' : 'production',
debug: isDev,
appName: name
})
log.info('Sentry initialized in main.')
} else {
log.debug('Crash reporting not enabled in config. Sentry not initialized in main.')
}
} catch (e) {
log.error('Initializing Sentry failed: ', e)
}
}

init({
enabled: process.env.NODE_ENV === 'production',
dsn: 'https://[email protected]/1210892',
environment: isDev ? 'development' : 'production',
debug: isDev,
appName: name
})
module.exports = initializeSentry
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
"electron-debug": "^3.0.1",
"electron-devtools-installer": "^3.1.1",
"electron-is-dev": "1.1.0",
"electron-log": "^3.0.7",
"electron-log": "^4.3.1",
"electron-next": "3.1.5",
"electron-unhandled": "^3.0.0",
"electron-updater": "^4.3.1",
Expand Down
22 changes: 13 additions & 9 deletions renderer/components/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MatomoProvider, createInstance } from '@datapunt/matomo-tracker-react'

import GlobalStyle from './globalStyle'
import MatomoTracker from './MatomoTracker'
import { init as initSentry } from '../components/initSentry'

let matomoInstance

Expand All @@ -17,15 +18,18 @@ if (typeof window !== 'undefined') {
})
}

const Layout = ({ children, analytics = true }) => (
<MatomoProvider value={matomoInstance}>
<Provider theme={theme}>
<GlobalStyle />
{analytics && <MatomoTracker />}
{children}
</Provider>
</MatomoProvider>
)
const Layout = ({ children, analytics = true }) => {
initSentry()
return (
<MatomoProvider value={matomoInstance}>
<Provider theme={theme}>
<GlobalStyle />
{analytics && <MatomoTracker />}
{children}
</Provider>
</MatomoProvider>
)
}

Layout.propTypes = {
children: PropTypes.element.isRequired,
Expand Down
66 changes: 41 additions & 25 deletions renderer/components/initSentry.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
import * as Sentry from '@sentry/node'
import { RewriteFrames } from '@sentry/integrations'
// import { ipcRenderer } from 'electron'
import log from 'electron-log'

export const init = () => {
if (process.env.NEXT_PUBLIC_SENTRY_DSN) {
const integrations = []
if (process.env.NEXT_IS_SERVER === 'true') {
// For Node.js, rewrite Error.stack to use relative paths, so that source
// maps starting with ~/_next map to files in Error.stack with path
// app:///_next
integrations.push(
new RewriteFrames({
iteratee: (frame) => {
frame.filename = frame.filename.replace(
__dirname,
'app:///'
)
frame.filename = frame.filename.replace('.next', '_next')
return frame
},
export const init = async () => {
try {
const { ipcRenderer } = require('electron')
const config = await ipcRenderer.invoke('get-fresh-config')
if (config && config.hasOwnProperty('advanced')
&& config.advanced.send_crash_reports === true
) {
log.info('Initializing Sentry in renderer...')
if (process.env.NEXT_PUBLIC_SENTRY_DSN) {
const integrations = []
if (process.env.NEXT_IS_SERVER === 'true') {
// For Node.js, rewrite Error.stack to use relative paths, so that source
// maps starting with ~/_next map to files in Error.stack with path
// app:///_next
integrations.push(
new RewriteFrames({
iteratee: (frame) => {
frame.filename = frame.filename.replace(
__dirname,
'app:///'
)
frame.filename = frame.filename.replace('.next', '_next')
return frame
},
})
)
}

Sentry.init({
enabled: process.env.NODE_ENV === 'production',
integrations,
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
release: process.env.NEXT_PUBLIC_COMMIT_SHA,
})
)
log.info('Sentry initialized in renderer.')
}
} else {
log.debug('Crash reporting not enabled in config. Sentry not initialized in renderer.')
}

Sentry.init({
enabled: process.env.NODE_ENV === 'production',
integrations,
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
release: process.env.NEXT_PUBLIC_COMMIT_SHA,
})
} catch (e) {
log.error('Initializing Sentry failed: ', e)
}
}
3 changes: 2 additions & 1 deletion renderer/pages/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const MyApp = ({ Component, pageProps, err }) => {

// Workaround for https://github.com/zeit/next.js/issues/8592
const modifiedPageProps = { ...pageProps, err }
initSentry()

// initSentry()

return (
<ConfigProvider>
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6141,10 +6141,10 @@ electron-localshortcut@^3.1.0:
keyboardevent-from-electron-accelerator "^2.0.0"
keyboardevents-areequal "^0.2.1"

electron-log@^3.0.7:
version "3.0.8"
resolved "https://registry.yarnpkg.com/electron-log/-/electron-log-3.0.8.tgz#ea672dc40b560143ed5d887eff1ff1904fe9ef71"
integrity sha512-B9+eJ8z3UbDnWEz+G33SIJvEDeKLznHEV4sCu6bR31KuOdp3dYN046QBWbLNsvKU+lzFI6eOi+xNCpNHZvatiw==
electron-log@^4.3.1:
version "4.3.1"
resolved "https://registry.yarnpkg.com/electron-log/-/electron-log-4.3.1.tgz#1405fef9d4e6964a5fdb8790a69163aa237ffe91"
integrity sha512-S/0CMjYjgyWUsZ3d27VvErPaI5W4oILp4jfeCuN4DhDqrJW6jKRUD2PxFfTdeZEIjM7+fttGg7A61rPcAcZC1w==

[email protected]:
version "3.1.5"
Expand Down

0 comments on commit 24b8738

Please sign in to comment.