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

feat(js): reload unhandledRejection and uncaughtException plugins on configure #1184

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Redundant now that the listener is removed in this case

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Redundant now that the listener is removed in this case.

I'd only included !this.__client here because typescript wouldn't let me check !this.__client.config.enableUnhandledRejection otherwise.

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 () {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are debating whether this is what we want in #1181, but I figured I'd document the current behavior.

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