Skip to content

Commit

Permalink
Merge pull request #2040 from bugsnag/PLAT-11169/electron-sync-layer-log
Browse files Browse the repository at this point in the history
  • Loading branch information
yousif-bugsnag authored Nov 20, 2023
2 parents ffe8d64 + 81080c9 commit 836db68
Show file tree
Hide file tree
Showing 17 changed files with 433 additions and 210 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

### Changed

- (react-native) Update bugsnag-android from v5.28.4 to [v5.31.3](https://github.com/bugsnag/bugsnag-cocoa/blob/master/CHANGELOG.md#5313-2023-11-06)
- (react-native) Update bugsnag-android from v5.28.4 to [v5.31.3](https://github.com/bugsnag/bugsnag-android/blob/master/CHANGELOG.md#5313-2023-11-06)
- (react-native) Update bugsnag-cocoa from v6.26.2 to [v6.27.3](https://github.com/bugsnag/bugsnag-cocoa/blob/master/CHANGELOG.md#6273-2023-11-15)

### Fixed

- (electron) Do not sync to NativeClient when `autoDetectErrors` or `nativeCrashes` are disabled [#2040](https://github.com/bugsnag/bugsnag-js/pull/2040)

## 7.22.1 (2023-10-31)

### Fixed
Expand Down
6 changes: 3 additions & 3 deletions packages/electron-test-helpers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ interface ClientTestHelpers {
export function makeClientForPlugin ({
config = {},
schema = {},
plugin = undefined
}: { config?: object, schema?: object, plugin?: Plugin } = {}): ClientTestHelpers {
plugins = []
}: { config?: object, schema?: object, plugins?: Plugin[] } = {}): ClientTestHelpers {
const client = new Client(
{
apiKey: 'abcabcabcabcabcabcabc1234567890f',
logger: { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() },
...config
},
{ ...defaultSchema, ...schema },
plugin !== undefined ? [plugin] : []
plugins
)

let lastSession: SessionPayload
Expand Down
38 changes: 27 additions & 11 deletions packages/plugin-electron-app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,43 @@ const native = require('bindings')('bugsnag_plugin_electron_app_bindings')
const { schema } = require('@bugsnag/core/config')
const intRange = require('@bugsnag/core/lib/validators/int-range')

const isNativeClientEnabled = client => client._config.autoDetectErrors && client._config.enabledErrorTypes.nativeCrashes

const noop = () => {}

const osToAppType = new Map([
['darwin', 'macOS'],
['linux', 'Linux'],
['win32', 'Windows']
])

const createAppUpdater = (client, NativeClient, app) => newProperties => {
Object.assign(app, newProperties)
const createAppUpdater = (client, NativeClient, app) => {
if (!isNativeClientEnabled(client)) {
return newProperties => Object.assign(app, newProperties)
}

return newProperties => {
Object.assign(app, newProperties)

try {
NativeClient.setApp(app)
} catch (err) {
client._logger.error(err)
try {
NativeClient.setApp(app)
} catch (err) {
client._logger.error(err)
}
}
}

const createLastRunInfoUpdater = (client, NativeClient) => lastRunInfo => {
try {
NativeClient.setLastRunInfo(JSON.stringify(lastRunInfo))
} catch (err) {
client._logger.error(err)
const createLastRunInfoUpdater = (client, NativeClient) => {
if (!isNativeClientEnabled(client)) {
return noop
}

return lastRunInfo => {
try {
NativeClient.setLastRunInfo(JSON.stringify(lastRunInfo))
} catch (err) {
client._logger.error(err)
}
}
}

Expand Down
51 changes: 48 additions & 3 deletions packages/plugin-electron-app/test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ describe('plugin: electron app info', () => {
expect(NativeClient.setApp).toHaveBeenCalledTimes(2)
})

it('does not sync "launchDurationMillis" elapsing after "markLaunchComplete" has been caled', async () => {
it('does not sync "launchDurationMillis" elapsing after "markLaunchComplete" has been called', async () => {
jest.useFakeTimers('modern')

const now = Date.now()
Expand Down Expand Up @@ -748,6 +748,38 @@ describe('plugin: electron app info', () => {
'Invalid configuration\n - launchDurationMillis should be an integer ≥0, got -1234567890'
))
})

it('does not sync data to NativeClient when enabledErrorTypes.nativeCrashes is disabled', async () => {
const NativeClient = makeNativeClient()
const config = { launchDurationMillis: 0, enabledErrorTypes: { nativeCrashes: false } }
const process = makeProcess({ platform: 'darwin' })
const { sendEvent, sendSession } = makeClient({ NativeClient, config, process })

// app info should still be updated in the JS client
const event = await sendEvent()
expect(event.app).toEqual(makeExpectedEventApp({ type: 'macOS' }))

const session = await sendSession()
expect(session.app).toEqual(makeExpectedSessionApp({ type: 'macOS' }))

expect(NativeClient.setApp).not.toHaveBeenCalled()
})

it('does not sync data to NativeClient when autoDetectErrors is disabled', async () => {
const NativeClient = makeNativeClient()
const config = { launchDurationMillis: 0, autoDetectErrors: false }
const process = makeProcess({ platform: 'darwin' })
const { sendEvent, sendSession } = makeClient({ NativeClient, config, process })

// app info should still be updated in the JS client
const event = await sendEvent()
expect(event.app).toEqual(makeExpectedEventApp({ type: 'macOS' }))

const session = await sendSession()
expect(session.app).toEqual(makeExpectedSessionApp({ type: 'macOS' }))

expect(NativeClient.setApp).not.toHaveBeenCalled()
})
})

interface MakeClientOptions {
Expand All @@ -756,10 +788,22 @@ interface MakeClientOptions {
NativeClient?: any
NativeApp?: any
process?: any
config?: { launchDurationMillis: number|undefined }
config?: object
filestore?: any
}

const schema = {
enabledErrorTypes: {
defaultValue: () => ({
unhandledExceptions: true,
unhandledRejections: true,
nativeCrashes: true
}),
allowPartialObject: true,
validate: value => true
}
}

function makeClient ({
BrowserWindow = makeBrowserWindow(),
electronApp = makeElectronApp({ BrowserWindow }),
Expand All @@ -771,7 +815,8 @@ function makeClient ({
}: MakeClientOptions = {}): ReturnType<typeof makeClientForPlugin> {
return makeClientForPlugin({
config,
plugin: plugin(NativeClient, process, electronApp, BrowserWindow, filestore, NativeApp)
schema,
plugins: [plugin(NativeClient, process, electronApp, BrowserWindow, filestore, NativeApp)]
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const featureFlagDelegate = require('@bugsnag/core/lib/feature-flag-delegate')

const isEnabledFor = client => client._config.autoDetectErrors && client._config.enabledErrorTypes.nativeCrashes

module.exports = {
NativeClient: require('bindings')('bugsnag_pecsp_bindings'),
plugin: (NativeClient) => ({
load: (client) => {
if (!isEnabledFor(client)) {
return
}

client.addOnBreadcrumb(breadcrumb => {
try {
NativeClient.leaveBreadcrumb(breadcrumb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"devDependencies": {
"@bugsnag/core": "^7.19.0",
"@bugsnag/plugin-electron-client-state-manager": "^7.19.0",
"@types/bindings": "^1.5.0"
"@types/bindings": "^1.5.0",
"@bugsnag/electron-test-helpers": "^7.19.0"
},
"peerDependencies": {
"@bugsnag/core": "^7.9.2"
Expand Down
Loading

0 comments on commit 836db68

Please sign in to comment.