Skip to content

Commit 79403a4

Browse files
committed
fallback if requestData cannot be parsed
1 parent 8ab9d40 commit 79403a4

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/index.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,19 +266,27 @@ function parseJSON(str: string) {
266266
}
267267
}
268268
function parseHttpEvent(event: APIGatewayProxyEventV2 | APIGatewayProxyEvent): HttpEvent {
269-
if (event.headers['content-type']?.toLowerCase() === 'application/json') {
269+
try {
270+
if (event.headers['content-type']?.toLowerCase() === 'application/json') {
271+
return {
272+
body: parseJSON(event.body || '{}'),
273+
headers: event.headers
274+
};
275+
}
276+
277+
/**
278+
* TODO: add support for other content types
279+
*/
280+
281+
return {
282+
body: event.body,
283+
headers: event.headers
284+
};
285+
} catch (_) {
270286
return {
271-
body: parseJSON(event.body || '{}'),
287+
body: event.body,
272288
headers: event.headers
273289
};
274290
}
275-
276-
/**
277-
* TODO: add support for other content types
278-
*/
279-
280-
return {
281-
body: event.body,
282-
headers: event.headers
283-
};
291+
284292
}

src/lambda-wrapper.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,26 @@ const instrumentations: Instrumentation[] = [
8888
try {
8989
requestBodyChunks.push(decodeURIComponent(data.toString()));
9090
} catch (e) {
91-
91+
9292
}
9393
}
9494
const headers = request.getHeaders();
9595

9696
const body: string = requestBodyChunks.join();
9797
let requestData: unknown
98-
if (headers['content-type'] && typeof headers['content-type'] === 'string') {
99-
if (headers['content-type'].includes('application/json') || headers['content-type'].includes('application/x-amz-json')) {
100-
try {
98+
try {
99+
if (headers['content-type'] && typeof headers['content-type'] === 'string') {
100+
if (headers['content-type'].includes('application/json') || headers['content-type'].includes('application/x-amz-json')) {
101101
requestData = JSON.parse(body);
102-
} catch (e) {
103-
requestData = body;
104-
}
105-
} else if (headers['content-type'].includes('application/x-www-form-urlencoded')) {
106-
try {
102+
} else if (headers['content-type'].includes('application/x-www-form-urlencoded')) {
107103
requestData = parse(body);
108-
} catch (e) {
109-
requestData = body;
110104
}
111-
requestData = body;
112105
}
106+
} catch (_) {
107+
requestData = body;
113108
}
114109

110+
115111
const httpReqData = {
116112
request: {
117113
headers,

0 commit comments

Comments
 (0)