Skip to content

🎥 Make videos programmatically with React

`@remotion/lambda`: Throw the response if a Lambda response is not valid JSON

Jonny Burgerbcbdf5133a56
diff --git a/packages/lambda/src/shared/call-lambda.ts b/packages/lambda/src/shared/call-lambda.ts
index 2842a8e830..fe6c676a8d 100644
--- a/packages/lambda/src/shared/call-lambda.ts
+++ b/packages/lambda/src/shared/call-lambda.ts
@@ -75,8 +75,16 @@ export const callLambda = async <T extends LambdaRoutines>({
return json;
};
+const parseJsonWithErrorSurfacing = (input: string) => {
+ try {
+ return JSON.parse(input);
+ } catch {
+ throw new Error(`Cannot parse Lambda response as JSON. Response: ${input}`);
+ }
+};
+
const parseJson = <T extends LambdaRoutines>(input: string) => {
- let json = JSON.parse(input) as
+ let json = parseJsonWithErrorSurfacing(input) as
| OrError<Awaited<LambdaReturnValues[T]>>
| {
errorType: string;
@@ -89,7 +97,7 @@ const parseJson = <T extends LambdaRoutines>(input: string) => {
};
if ('statusCode' in json) {
- json = JSON.parse(json.body) as
+ json = parseJsonWithErrorSurfacing(json.body) as
| OrError<Awaited<LambdaReturnValues[T]>>
| {
errorType: string;