Skip to content
This repository was archived by the owner on Nov 27, 2019. It is now read-only.

Commit 6407b4d

Browse files
authored
Merge pull request #7 from Travelport-Ukraine/error-info
Error info and base64
2 parents 1b5b94b + 97766de commit 6407b4d

File tree

5 files changed

+125
-7
lines changed

5 files changed

+125
-7
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ module.exports.lambda = R((data) => {
2323
// return Promise.reject(new Error('Some error')); // = 500 status and correct errorMessage
2424

2525
// or
26-
// return Promise.reject({ data:false }); // = 500 status and UnkownError name and message
26+
// return Promise.reject({ data:false }); // = 500 status and UnkownError name and message
2727

2828
// or
2929
// throw Error('Some error'); // will be handled as error and status = 500
30+
31+
// or
32+
// const e = new Error('Not found');
33+
// e.statusCode = 404;
34+
// e.headers = { 'NotFoundToken': 12345 }
35+
// throw e;
3036
});
3137
```
3238

lib/index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ const calculateExecution = endTime =>
55

66
const isEmpty = v => v === null || v === undefined;
77

8+
const extractEventData = (event) => {
9+
if (!event) {
10+
return {};
11+
}
12+
13+
if (event.body) {
14+
const body = event.isBase64Encoded ? Buffer.from(event.body, 'base64').toString() : event.body;
15+
16+
return JSON.parse(body);
17+
}
18+
19+
return event.queryStringParameters;
20+
};
21+
822
/**
923
* This function is used to handle params of aws request and process them
1024
*
@@ -42,7 +56,8 @@ function R(...params) {
4256

4357
return async (event, context) => {
4458
const startTime = process.hrtime();
45-
const data = event && event.body ? JSON.parse(event.body) : event.queryStringParameters || {};
59+
const data = extractEventData(event);
60+
4661
let transformed = null;
4762

4863
try {
@@ -64,7 +79,7 @@ function R(...params) {
6479

6580
const { headers = {}, ...args } = options;
6681

67-
const requestBody = {
82+
return {
6883
statusCode: 200,
6984
headers: {
7085
'Access-Control-Allow-Origin': '*',
@@ -83,13 +98,12 @@ function R(...params) {
8398
}),
8499
...args,
85100
};
86-
87-
return requestBody;
88101
} catch (err) {
89102
return {
90-
statusCode: 500,
103+
statusCode: err.statusCode || 500,
91104
headers: {
92105
'Access-Control-Allow-Origin': '*',
106+
...(err.headers ? err.headers : null),
93107
},
94108
body: JSON.stringify({
95109
status: 'error',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aws-response",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "AWS response handler",
55
"main": "lib/index.js",
66
"scripts": {

test/index.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,51 @@ describe('Test handling request', () => {
312312
}
313313
});
314314
});
315+
316+
it('should return custom status code and headers from error', () => {
317+
const event = require('./sample-requests/GET-request-aws.json');
318+
319+
const handler = R((data) => {
320+
expect(data).to.be.deep.equal(
321+
Object.assign(
322+
{},
323+
event.queryStringParameters,
324+
{
325+
path1: 'ok',
326+
},
327+
{
328+
authorizer: undefined,
329+
headers: lowercaseKeys(event.headers),
330+
context: { logStreamName: '1', awsRequestId: '1' },
331+
}
332+
)
333+
);
334+
335+
const error = new Error();
336+
error.statusCode = 405;
337+
error.headers = { 'X-API': '1.0.0' };
338+
339+
throw error;
340+
});
341+
342+
return handler(event, { logStreamName: '1', awsRequestId: '1' }).then((result) => {
343+
expect(result.headers).to.be.an('object');
344+
expect(result.headers).to.have.all.keys('Access-Control-Allow-Origin', 'X-API');
345+
expect(result.headers['X-API']).to.be.equal('1.0.0');
346+
expect(result.statusCode).to.be.equal(405);
347+
});
348+
});
349+
350+
it('should be able to decode base64 incoming request', () => {
351+
const event = require('./sample-requests/GET-request-aws-base64encoded.json');
352+
353+
const handler = R((data) => {
354+
expect(data.some).to.be.eq('json'); // base64 decoded json
355+
});
356+
357+
return handler(event, { logStreamName: '1', awsRequestId: '1' }).then((result) => {
358+
expect(result.headers).to.be.an('object');
359+
expect(result.headers).to.have.all.keys('Access-Control-Allow-Origin');
360+
});
361+
});
315362
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"body": "eyAic29tZSI6ICJqc29uIiB9",
3+
"headers": {
4+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
5+
"Accept-Encoding": "gzip, deflate, br",
6+
"Accept-Language": "en-US,en;q=0.8,ru;q=0.6,uk;q=0.4",
7+
"CloudFront-Forwarded-Proto": "https",
8+
"CloudFront-Is-Desktop-Viewer": "true",
9+
"CloudFront-Is-Mobile-Viewer": "false",
10+
"CloudFront-Is-SmartTV-Viewer": "false",
11+
"CloudFront-Is-Tablet-Viewer": "false",
12+
"CloudFront-Viewer-Country": "UA",
13+
"Host": "",
14+
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3076.0 Safari/537.36",
15+
"Via": "",
16+
"X-Amz-Cf-Id": "",
17+
"X-Amzn-Trace-Id": "",
18+
"X-Forwarded-For": "37.73.233.176, 54.239.171.44",
19+
"X-Forwarded-Port": "443",
20+
"X-Forwarded-Proto": "https",
21+
"upgrade-insecure-requests": "1"
22+
},
23+
"httpMethod": "GET",
24+
"isBase64Encoded": true,
25+
"path": "/first",
26+
"requestContext": {
27+
"accountId": "",
28+
"apiId": "",
29+
"httpMethod": "GET",
30+
"identity": {
31+
"accessKey": null,
32+
"accountId": null,
33+
"apiKey": null,
34+
"caller": null,
35+
"cognitoAuthenticationProvider": null,
36+
"cognitoAuthenticationType": null,
37+
"cognitoIdentityId": null,
38+
"cognitoIdentityPoolId": null,
39+
"sourceIp": "37.73.233.176",
40+
"user": null,
41+
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3076.0 Safari/537.36",
42+
"userArn": null
43+
},
44+
"requestId": "4073894c-2a60-11e7-aa0b-3b98237f23f7",
45+
"resourceId": "",
46+
"resourcePath": "/first",
47+
"stage": "dev"
48+
},
49+
"resource": "/first",
50+
"stageVariables": null
51+
}

0 commit comments

Comments
 (0)