Skip to content

Commit

Permalink
feat(js): reload unhandledRejection and uncaughtException plugins on …
Browse files Browse the repository at this point in the history
…configure (#1184)
  • Loading branch information
BethanyBerkowitz authored Sep 8, 2023
1 parent 54fbbb0 commit 9f1591e
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 41 deletions.
12 changes: 6 additions & 6 deletions packages/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ export default class UncaughtExceptionMonitor {
makeListener() {
const honeybadgerUncaughtExceptionListener = (uncaughtError: Error) => {
if (this.__isReporting || !this.__client) { return }

if (!this.__client.config.enableUncaught) {
this.__client.config.afterUncaught(uncaughtError)
if (!this.hasOtherUncaughtExceptionListeners()) {
fatallyLogAndExit(uncaughtError)
}
return
}

// report only the first error - prevent reporting recursive errors
if (this.__handlerAlreadyCalled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function (): Types.Plugin {
} else {
uncaughtExceptionMonitor.maybeRemoveListener()
}
}
},
shouldReloadOnConfigure: true,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ export default class UnhandledRejectionMonitor {

makeListener() {
const honeybadgerUnhandledRejectionListener = (reason: unknown, _promise: Promise<unknown>) => {
if (!this.__client || !this.__client.config.enableUnhandledRejection) {
if (!this.hasOtherUnhandledRejectionListeners() && !this.__isReporting) {
fatallyLogAndExit(reason as Error)
}
return
}

this.__isReporting = true;
this.__client.notify(reason as Types.Noticeable, { component: 'unhandledRejection' }, {
afterNotify: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function (): Types.Plugin {
} else {
unhandledRejectionMonitor.maybeRemoveListener()
}
}
},
shouldReloadOnConfigure: true,
}
}
25 changes: 25 additions & 0 deletions packages/js/test/unit/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,29 @@ describe('server client', function () {
expect(request.isDone()).toBe(true)
})
})

describe('__plugins', function () {
it('exported singleton includes plugins', function () {
Singleton.configure({ apiKey: 'foo' })
expect(Singleton.config.__plugins.length).toBe(2)
})

it('clients produced via factory don\'t include plugins', function () {
client.configure({ apiKey: 'foo' })
expect(client.config.__plugins.length).toBe(0)
})

// Integration test with the plugins themselves
it('uncaughtException and unhandledRejection plugins reload on configure', function () {
function getListenerCount(type) {
return process.listeners(type).length
}
Singleton.configure({ apiKey: 'foo' })
expect(getListenerCount('uncaughtException')).toBe(1)
expect(getListenerCount('unhandledRejection')).toBe(1)
Singleton.configure({ enableUncaught: false, enableUnhandledRejection: false })
expect(getListenerCount('uncaughtException')).toBe(0)
expect(getListenerCount('unhandledRejection')).toBe(0)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ describe('Uncaught Exception Plugin', () => {

it('is a function which returns an object with a load function', () => {
expect(plugin()).toStrictEqual({
load: expect.any(Function)
load: expect.any(Function),
shouldReloadOnConfigure: true,
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,6 @@ describe('UnhandledRejectionMonitor', () => {
done()
})
})

it('exits if enableUnhandledRejection is false and there are no other listeners', () => {
client.configure({ enableUnhandledRejection: false })
unhandledRejectionMonitor.__listener(reason, promise)
expect(notifySpy).not.toHaveBeenCalled()
expect(fatallyLogAndExitSpy).toHaveBeenCalledWith(reason)
})

it('returns if enableUnhandledRejection is false and there are other listeners', () => {
process.on('unhandledRejection', () => true)
process.on('unhandledRejection', () => true)
client.configure({ enableUnhandledRejection: false })
unhandledRejectionMonitor.__listener(reason, promise)
expect(notifySpy).not.toHaveBeenCalled()
expect(fatallyLogAndExitSpy).not.toHaveBeenCalled()
})
})

describe('hasOtherUnhandledRejectionListeners', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ describe('Uncaught Exception Plugin', () => {

it('is a function which returns an object with a load function', () => {
expect(plugin()).toStrictEqual({
load: expect.any(Function)
load: expect.any(Function),
shouldReloadOnConfigure: true,
})
})

Expand Down

0 comments on commit 9f1591e

Please sign in to comment.