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(plugin-koa): Ensure unhandled Koa errors are logged out #614

Merged
merged 2 commits into from
Aug 16, 2019
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
7 changes: 7 additions & 0 deletions packages/plugin-koa/src/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ module.exports = {
ctx.bugsnag.notify(createReportFromErr(err, handledState))
}
if (!ctx.response.headerSent) ctx.response.status = err.status || 500
try {
// this function will throw if you give it a non-error, but we still want
// to output that, so if it throws, pass it back what it threw (a TypeError)
ctx.app.onerror(err)
} catch (e) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ctx.app.onerror(e)
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions packages/plugin-koa/test/koa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,22 @@ describe('plugin: koa', () => {
expect(typeof middleware.errorHandler).toBe('function')
expect(middleware.errorHandler.length).toBe(2)
})

describe('requestHandler', () => {
it('should call through to app.onerror to ensure the error is logged out', (done) => {
const c = new Client(VALID_NOTIFIER)
c.setOptions({ apiKey: 'api_key' })
c.configure()
c.use(plugin)
const middleware = c.getPlugin('koa')
const mockCtx = {
req: { connection: { address: () => ({ port: 1234 }) } },
request: { query: {} },
res: {},
response: { headerSent: false },
app: { onerror: () => done() }
}
middleware.requestHandler(mockCtx)
})
})
})