Skip to content

Commit

Permalink
fix: 🐛 ensure reportUnhandledPromiseRejectionsAsHandled is impemented…
Browse files Browse the repository at this point in the history
… in all platforms
  • Loading branch information
gingerbenw committed Oct 24, 2024
1 parent 356951b commit ec6216a
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 12 deletions.
16 changes: 11 additions & 5 deletions packages/browser/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BugsnagBrowserStatic, { Breadcrumb, Session } from '../src/notifier'
import BugsnagBrowserStatic, { Breadcrumb, BrowserConfig, Session } from '../src/notifier'

const DONE = window.XMLHttpRequest.DONE

Expand Down Expand Up @@ -137,7 +137,8 @@ describe('browser notifier', () => {

it('accepts all config options', (done) => {
const Bugsnag = getBugsnag()
Bugsnag.start({

const completeConfig: Required<BrowserConfig> = {
apiKey: API_KEY,
appVersion: '1.2.3',
appType: 'worker',
Expand All @@ -161,18 +162,23 @@ describe('browser notifier', () => {
releaseStage: 'production',
maxBreadcrumbs: 20,
enabledBreadcrumbTypes: ['manual', 'log', 'request'],
context: 'contextual',
featureFlags: [],
plugins: [],
user: null,
metadata: {
debug: { foo: 'bar' }
},
logger: undefined,
logger: { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() },
redactedKeys: ['foo', /bar/],
collectUserIp: true,
maxEvents: 10,
generateAnonymousId: false,
trackInlineScripts: true
})
trackInlineScripts: true,
reportUnhandledPromiseRejectionsAsHandled: true
}

Bugsnag.start(completeConfig)
Bugsnag.notify(new Error('123'), (event) => {
return false
}, (err, event) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/electron/test/type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ describe.skip('@bugsnag/electron types', () => {
plugins: [],
user: { id: '1234-abcd' },
appType: 'good',
codeBundleId: '1245'
codeBundleId: '1245',
reportUnhandledPromiseRejectionsAsHandled: true
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const stringWithLength = require('@bugsnag/core/lib/validators/string-with-lengt
const rnPackage = require('react-native/package.json')
const iserror = require('iserror')

const ALLOWED_IN_JS = ['onError', 'onBreadcrumb', 'logger', 'metadata', 'user', 'context', 'codeBundleId', 'plugins', 'featureFlags']
const ALLOWED_IN_JS = ['onError', 'onBreadcrumb', 'logger', 'metadata', 'user', 'context', 'codeBundleId', 'plugins', 'featureFlags', 'reportUnhandledPromiseRejectionsAsHandled']
const allowedErrorTypes = () => ({
unhandledExceptions: true,
unhandledRejections: true,
Expand Down
8 changes: 8 additions & 0 deletions packages/react-native/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ describe('react native notifier', () => {
expect(NativeModules.BugsnagReactNative.addFeatureFlags).toHaveBeenCalled()
})

it('accepts the reportUnhandledPromiseRejectionsAsHandled config option', () => {
const warnMock = jest.spyOn(console, 'warn').mockImplementation(() => {})

Bugsnag.start({ reportUnhandledPromiseRejectionsAsHandled: true })

expect(warnMock).not.toHaveBeenCalled()
})

describe('isStarted()', () => {
it('returns false when the notifier has not been initialized', () => {
expect(Bugsnag.isStarted()).toBe(false)
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/types/bugsnag.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface ReactNativeSchema extends Config {
}

// these properties are allowed to be configured in the JS layer
type Configurable = 'onError' | 'onBreadcrumb' | 'logger' | 'metadata' | 'user' | 'context' | 'plugins' | 'codeBundleId' | 'featureFlags'
type Configurable = 'onError' | 'onBreadcrumb' | 'logger' | 'metadata' | 'user' | 'context' | 'plugins' | 'codeBundleId' | 'featureFlags' | 'reportUnhandledPromiseRejectionsAsHandled'

type ReactNativeConfig = Pick<ReactNativeSchema, Configurable>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var Bugsnag = require('@bugsnag/node')
Bugsnag.start({
reportUnhandledPromiseRejectionsAsHandled: true,
apiKey: process.env.BUGSNAG_API_KEY,
endpoints: {
notify: process.env.BUGSNAG_NOTIFY_ENDPOINT,
sessions: process.env.BUGSNAG_SESSIONS_ENDPOINT
}
})

Promise.reject(new Error('not handled'))
8 changes: 4 additions & 4 deletions test/node/features/unhandled_errors.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ Scenario: reporting unhandled promise rejections
And the "file" of stack frame 0 equals "scenarios/unhandled-promise-rejection.js"
And the "lineNumber" of stack frame 0 equals 10

Scenario: reporting unhandled promise rejections
And I run the service "unhandled" with the command "node scenarios/unhandled-promise-rejection"
Scenario: reporting unhandled promise rejections as handled
And I run the service "unhandled" with the command "node scenarios/unhandled-promise-rejection-as-handled"
And I wait to receive an error
Then the error is valid for the error reporting API version "4" for the "Bugsnag Node" notifier
And the event "unhandled" is true
And the event "unhandled" is false
And the event "severity" equals "error"
And the event "severityReason.type" equals "unhandledPromiseRejection"
And the exception "errorClass" equals "Error"
And the exception "message" equals "not handled"
And the exception "type" equals "nodejs"
And the "file" of stack frame 0 equals "scenarios/unhandled-promise-rejection.js"
And the "file" of stack frame 0 equals "scenarios/unhandled-promise-rejection-as-handled.js"
And the "lineNumber" of stack frame 0 equals 10

Scenario: not reporting unhandledRejections when autoDetectErrors is off
Expand Down

0 comments on commit ec6216a

Please sign in to comment.