Skip to content
This repository was archived by the owner on Aug 8, 2024. It is now read-only.

Commit e137ef6

Browse files
authored
Merge pull request #3 from WeltN24/fix_error_handling
fixes for better errorhandling
2 parents f1a7e02 + 9f562e6 commit e137ef6

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

lib/proxyIntegration.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ const NO_MATCHING_ACTION = request => {
77
}
88
};
99

10+
function addCorsHeaders(toAdd) {
11+
toAdd["Access-Control-Allow-Origin"] = "*";
12+
toAdd["Access-Control-Allow-Methods"] = "GET,POST,PUT,DELETE,HEAD";
13+
toAdd["Access-Control-Allow-Headers"] = "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token";
14+
return toAdd;
15+
}
16+
1017
function process(proxyIntegrationConfig, event) {
1118
//validate config
1219
if (!Array.isArray(proxyIntegrationConfig.routes) || proxyIntegrationConfig.routes.length < 1) {
@@ -20,9 +27,7 @@ function process(proxyIntegrationConfig, event) {
2027

2128
let headers = {};
2229
if (proxyIntegrationConfig.cors) {
23-
headers["Access-Control-Allow-Origin"] = "*";
24-
headers["Access-Control-Allow-Methods"] = "GET,POST,PUT,DELETE,HEAD";
25-
headers["Access-Control-Allow-Headers"] = "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token";
30+
addCorsHeaders(headers);
2631
if (event.httpMethod === 'OPTIONS') {
2732
return Promise.resolve({statusCode: 200, headers: headers, body: ''});
2833
}
@@ -46,11 +51,19 @@ function process(proxyIntegrationConfig, event) {
4651
try {
4752
event.body = JSON.parse(event.body);
4853
} catch (parseError) {
49-
return Promise.resolve({statusCode: 400, headers: headers, body: 'body is not a valid JSON'});
54+
return Promise.resolve({
55+
statusCode: 400,
56+
headers: headers,
57+
body: 'body is not a valid JSON'
58+
});
5059
}
5160
}
5261
return Promise.resolve(actionConfig.action(event)).then(res => {
53-
return {statusCode: 200, headers: headers, body: JSON.stringify(res)};
62+
return {
63+
statusCode: 200,
64+
headers: headers,
65+
body: JSON.stringify(res)
66+
};
5467
}).catch(err => {
5568
return convertError(err, errorMapping, headers);
5669
});
@@ -64,7 +77,7 @@ function normalizeRequestPath(event) {
6477
// ugly hack: if host is from API-Gateway 'Custom Domain Name Mapping', then event.path has the value '/basepath/resource-path/';
6578
// if host is from amazonaws.com, then event.path is just '/resource-path':
6679
const apiId = event.requestContext ? event.requestContext.apiId : null; // the apiId that is the first part of the amazonaws.com-host
67-
if ((apiId && event.headers && event.headers.Host && event.headers.Host.substring(0, apiId.length) != apiId)) {
80+
if ((apiId && event.headers && event.headers.Host && event.headers.Host.substring(0, apiId.length) !== apiId)) {
6881
// remove first path element:
6982
const groups = /\/[^\/]+(.*)/.exec(event.path) || [null, null];
7083
return groups[1] || '/';
@@ -74,15 +87,27 @@ function normalizeRequestPath(event) {
7487
}
7588

7689
function convertError(error, errorMapping, headers) {
77-
if (typeof error.reason === 'string' && typeof error.message === 'string' && errorMapping && errorMapping[error.reason]) {
78-
return {statusCode: errorMapping[error.reason], body: error.message, headers: headers};
79-
}
80-
return {statusCode: 500, body: `Generic error: ${JSON.stringify(error)}`};
90+
if (typeof error.reason === 'string' && errorMapping && errorMapping[error.reason]) {
91+
return {statusCode: errorMapping[error.reason], body: JSON.stringify(error.message), headers: headers};
92+
}else if(typeof error.status === 'number'){
93+
return {statusCode: error.status, body: JSON.stringify(error.message), headers: addCorsHeaders({})};
94+
}
95+
try{
96+
return {
97+
statusCode: 500,
98+
body: `Generic error: ${JSON.stringify(error)}`,
99+
headers: addCorsHeaders({})
100+
};
101+
}catch(stringifyError){}
102+
return {
103+
statusCode: 500,
104+
body: `Generic error: ${error.stack ? error.stack : error}`
105+
};
81106
}
82107

83108
function findMatchingActionConfig(httpMethod, httpPath, routeConfig) {
84109
const paths = {};
85-
const matchingMethodRoutes = routeConfig.routes.filter(route => route.method == httpMethod);
110+
const matchingMethodRoutes = routeConfig.routes.filter(route => route.method === httpMethod);
86111
for (let route of matchingMethodRoutes) {
87112
if (routeConfig.debug) {
88113
console.log(`Examining route ${route.path} to match ${httpPath}`);

test/proxyIntegration.spec.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ describe('proxyIntegration.routeHandler', () => {
306306
proxyIntegration(routeConfig, {path: '/', httpMethod: 'GET'}).then(result => {
307307
expect(result).toEqual({
308308
statusCode: 501,
309-
body: 'bla',
309+
body: '"bla"',
310310
headers: Object.assign({"Content-Type": "application/json"}, expectedCorsHeaders)
311311
});
312312
done();
@@ -328,7 +328,30 @@ describe('proxyIntegration.routeHandler', () => {
328328
proxyIntegration(routeConfig, {path: '/', httpMethod: 'GET'}).then(result => {
329329
expect(result).toEqual({
330330
statusCode: 500,
331-
body: 'Generic error: ' + JSON.stringify(incorrectError)
331+
body: 'Generic error: ' + JSON.stringify(incorrectError),
332+
headers: expectedCorsHeaders
333+
});
334+
done();
335+
});
336+
});
337+
it('should pass through error statuscode', (done) => {
338+
const statusCodeError = {status: 666, message: {reason: 'oops'}};
339+
const routeConfig = {
340+
routes: [
341+
{
342+
method: 'GET',
343+
path: '/',
344+
action: () => {
345+
throw statusCodeError
346+
}
347+
}
348+
]
349+
};
350+
proxyIntegration(routeConfig, {path: '/', httpMethod: 'GET'}).then(result => {
351+
expect(result).toEqual({
352+
statusCode: 666,
353+
body: '{"reason":"oops"}',
354+
headers: expectedCorsHeaders
332355
});
333356
done();
334357
});
@@ -361,7 +384,7 @@ describe('proxyIntegration.routeHandler.returnvalues', () => {
361384
proxyIntegration(routeConfig, {path: '/', httpMethod: 'GET'}).then(result => {
362385
expect(result).toEqual({
363386
statusCode: 599,
364-
body: 'doof',
387+
body: '"doof"',
365388
headers: {"Content-Type": "application/json"}
366389
});
367390
done();

0 commit comments

Comments
 (0)