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

Fix the Koa plugin suppressing other error handlers #1482

Merged
merged 12 commits into from
Jul 21, 2021

Conversation

imjoehaines
Copy link
Member

Goal

The Koa plugin currently works by catching exceptions in the request handler and notifying Bugsnag. This works but prevents Koa from detecting that there was an error and so it doesn't trigger error handlers to run. This also means that Bugsnag's error handler will never be called

This PR overhauls the plugin so that it works primarily through the error handler, not the request handler. The request handler still exists to attach request data as metadata and create ctx.bugsnag, but does nothing else. The error handler will then be triggered by Koa when an error is thrown and Koa will then ensure all other error handlers also run

The error handler is now more complicated as logic from the request handler has been moved into it. Specifically:

Finally we no longer set the status code ourselves as Koa's default error handling does that already. Assertions against the status code have been added to the MazeRunner tests

Testing

Unit tests have been added for most functionality (where it makes sense) and the Maze Runner tests have been updated to ensure other error handlers run

Koa internally wraps thrown non-Errors in an Error instance, which
means we no longer detect a non-Error in Bugsnag

This doesn't seem like a problem as it's still clearly not an Error
instance originally:

> non-error thrown: "<thing that was  thrown>"
There's no need to attach metadata if ctx.bugsnag exists as we know
we have added an onError callback to record the metadata already

This was previously required as the errorHandler and requestHandler
were totally independant
We no longer set the status code internally, but Koa does this in
its error handler so we don't need to anymore
Koa doesn't add its own error handler if another handler has been
added already. We don't want to suppress the default handler just
by adding Bugsnag, so we need to manually call the built-in handler

However, if another handler has already been added then we don't
want to call the default as that is also a change in behaviour
Comment on lines +77 to +78
And the exception "errorClass" equals "Error"
And the exception "message" equals 'non-error thrown: \\"error\\"'
Copy link
Member Author

Choose a reason for hiding this comment

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

As we rely on Koa catching errors now, it will internally wrap them in an Error instance before they reach Bugsnag (see Koa's context.js). This changes the error class and message, but is otherwise equivalent

@tomlongridge tomlongridge requested review from djskinner and removed request for tomlongridge July 20, 2021 10:10
@github-actions
Copy link

github-actions bot commented Jul 20, 2021

@bugsnag/browser bundle size diff

Minified Minfied + Gzipped
Before 41.24 kB 12.71 kB
After 41.24 kB 12.71 kB
± No change No change

code coverage diff

Ok File Lines Branches Functions Statements
/home/runner/work/bugsnag-js/bugsnag-js/packages/plugin-koa/src/koa.js 82.05%
(+28.05%)
81.25%
(+56.25%)
71.43%
(+14.29%)
82.05%
(+31.14%)
/home/runner/work/bugsnag-js/bugsnag-js/packages/plugin-koa/src/request-info.js 100%
(+0%)
78.95%
(+5.27%)
100%
(+0%)
88.89%
(+0%)

Total:

Lines Branches Functions Statements
82.58%(+0.33%) 72.02%(+0.76%) 83.57%(+0.09%) 81.56%(+0.37%)

Generated by 🚫 dangerJS against 8cdad3d

@imjoehaines imjoehaines requested review from tomlongridge and djskinner and removed request for djskinner and tomlongridge July 20, 2021 10:16
@imjoehaines imjoehaines force-pushed the fix-koa-not-propagating-errors branch from 1fc4c0d to 82be592 Compare July 20, 2021 12:49
@imjoehaines imjoehaines force-pushed the fix-koa-not-propagating-errors branch from 82be592 to febca91 Compare July 20, 2021 14:25
@imjoehaines imjoehaines force-pushed the fix-koa-not-propagating-errors branch from a70c229 to 8cdad3d Compare July 20, 2021 16:30
@imjoehaines imjoehaines merged commit e75fa7c into next Jul 21, 2021
@imjoehaines imjoehaines deleted the fix-koa-not-propagating-errors branch July 21, 2021 09:01
@djskinner djskinner mentioned this pull request Jul 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants