Skip to content

Commit 9763ca6

Browse files
authored
Merge pull request #227 from dominiklessel/boom-support
feat: add support for Boom errors
2 parents d971258 + 1fdf9ba commit 9763ca6

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

.projen/deps.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projen/tasks.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projenrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const project = new TaimosTypescriptLibrary({
2020
'@types/jwk-to-pem',
2121
'@types/uuid',
2222
'@taimos/projen',
23+
'@hapi/boom',
2324
],
2425
keywords: [
2526
'aws',

package.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/http/handler.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@ export const createHttpHandler =
9494
body: error.message,
9595
};
9696
}
97+
if (error.isBoom) {
98+
return {
99+
statusCode: error.output.statusCode,
100+
headers: {
101+
'Content-Type': 'application/json',
102+
...error.output.headers,
103+
...corsHeader(event),
104+
...ctx.response.headers,
105+
},
106+
body: JSON.stringify(error.output.payload),
107+
};
108+
}
97109
ctx.logger.error(error);
98110
return {
99111
statusCode: ctx.response.statusCode ?? 500,

test/boom.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as Boom from '@hapi/boom';
2+
import { createHttpHandler } from '../src/http/handler';
3+
4+
test('Boom error support for createHttpHandler', async () => {
5+
const boomErr = Boom.notImplemented();
6+
const handler = createHttpHandler(async () => {
7+
throw boomErr;
8+
});
9+
const event = {
10+
headers: {},
11+
requestContext: {},
12+
version: '1',
13+
routeKey: 'routeKey',
14+
rawPath: '/raw/path',
15+
rawQueryString: '',
16+
isBase64Encoded: false,
17+
} as AWSLambda.APIGatewayProxyEventV2;
18+
const context = {} as AWSLambda.Context;
19+
const result = await handler(event, context, () => {});
20+
21+
if (!result) {
22+
throw new Error('handler is expected to return a result!');
23+
}
24+
25+
expect(result.statusCode).toBe(501);
26+
expect(result.headers).not.toBeUndefined();
27+
28+
if (!result.body) {
29+
throw new Error('result.body is expected to be defined!');
30+
}
31+
32+
expect(JSON.parse(result.body)).toEqual(boomErr.output.payload);
33+
});

yarn.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)