Skip to content

Commit

Permalink
Log returned error responses in Koa template
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c committed May 27, 2021
1 parent 1742096 commit e4155d9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-experts-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

**template/koa-rest-api:** Log returned error responses
40 changes: 37 additions & 3 deletions template/koa-rest-api/src/framework/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ describe('createApp', () => {

expect(rootLogger.error).not.toBeCalled();

expect(rootLogger.info).not.toBeCalled();
expect(rootLogger.info).nthCalledWith(
1,
expect.objectContaining({ status: 404 }),
'Client error',
);

metricsClient.expectTagSubset([
'http_method:get',
Expand All @@ -84,7 +88,37 @@ describe('createApp', () => {
]);
});

it('handles client error', async () => {
it('handles returned client error', async () => {
const message = chance.sentence();

middleware.mockImplementation((ctx) => {
ctx.body = message;
ctx.status = 400;
});

await agent
.get('/')
.expect(400, message)
.expect('server', /.+/)
.expect('x-api-version', /.+/);

expect(rootLogger.error).not.toBeCalled();

expect(rootLogger.info).nthCalledWith(
1,
expect.objectContaining({ status: 400 }),
'Client error',
);

metricsClient.expectTagSubset([
'http_method:get',
'http_status:400',
'http_status_family:4xx',
'route:/',
]);
});

it('handles client error thrown from context', async () => {
const message = chance.sentence();

middleware.mockImplementation((ctx) => ctx.throw(400, message));
Expand All @@ -111,7 +145,7 @@ describe('createApp', () => {
]);
});

it('handles server error', async () => {
it('handles server error thrown from context', async () => {
const message = chance.sentence();

middleware.mockImplementation((ctx) => ctx.throw(500, message));
Expand Down
2 changes: 1 addition & 1 deletion template/koa-rest-api/src/framework/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const metrics = MetricsMiddleware.create(
);

const requestLogging = RequestLogging.createMiddleware((ctx, fields, err) => {
if (err === undefined) {
if (ctx.status < 400 && err === undefined) {
// Depend on sidecar logging for happy path requests
return;
}
Expand Down

0 comments on commit e4155d9

Please sign in to comment.