Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLAT-5467] fix(react-native): Ensure unhandled promise rejections are logged out #1235

Merged
merged 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Automatically notifies Bugsnag when an unhandled promise rejection happens in React Native
*/

/* global __DEV__ */

const rnPromise = require('promise/setimmediate/rejection-tracking')

module.exports = {
Expand All @@ -16,8 +18,20 @@ module.exports = {
severityReason: { type: 'unhandledPromiseRejection' }
}, 'promise rejection tracking', 1)
client._notify(event)
// adding our own onUnhandled callback means the default log message doesn't happen, so make it happen here
if (typeof __DEV__ !== 'undefined' && __DEV__) logError(id, error)
}
})
return () => rnPromise.disable()
}
}

// this function is copied in from the promise module, since it's not exported and we can't reference it:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we need to add the license here too; I guess this isn't a "substantial portion" so maybe not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't seem that substantial to me but IANAL 🤷

// https://github.com/then/promise/blob/91b7b4cb6ad0cacc1c70560677458fe0aac2fa67/src/rejection-tracking.js#L101-L107
function logError (id, error) {
console.warn('Possible Unhandled Promise Rejection (id: ' + id + '):')
var errStr = (error && (error.stack || error)) + ''
errStr.split('\n').forEach(function (line) {
console.warn(' ' + line)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@ import Client from '@bugsnag/core/client'
// @ts-ignore
import RnPromise from 'promise/setimmediate'

beforeEach(() => {
// @ts-ignore
global.__DEV__ = true
jest.spyOn(console, 'warn')
})

afterEach(() => {
jest.restoreAllMocks()
})

describe('plugin: react native rejection handler', () => {
it('should hook in to the promise rejection tracker', (done) => {
expect.assertions(4)
expect.assertions(5)

const c = new Client({ apiKey: 'api_key' })
c._setDelivery(client => ({
Expand All @@ -18,7 +28,10 @@ describe('plugin: react native rejection handler', () => {
expect(r.events[0].severity).toBe('error')
expect(r.events[0].severityReason).toEqual({ type: 'unhandledPromiseRejection' })
expect(r.events[0].unhandled).toBe(true)
done()
setTimeout(() => {
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('Possible Unhandled Promise Rejection'))
done()
}, 0)
},
sendSession: () => {}
}))
Expand Down