Skip to content

Commit 9802491

Browse files
authored
fix: Add error handling for lambda invocations (#152)
1 parent 1b9c817 commit 9802491

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/getAppSyncConfig.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,33 @@ export default function getAppSyncConfig(context, appSyncConfig) {
126126
headers: payload?.request?.headers,
127127
validateStatus: false,
128128
});
129-
return result.data;
129+
// When the Lambda returns an error, the status code is 200 OK.
130+
// The presence of an error is indicated by a header in the response.
131+
// 400 and 500-series status codes are reserved for invocation errors:
132+
// https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_Errors
133+
if (result.status === 200) {
134+
const errorType =
135+
result.headers['x-amz-function-error'] ||
136+
result.headers['X-Amz-Function-Error'] ||
137+
result.headers['x-amzn-errortype'] ||
138+
result.headers['x-amzn-ErrorType'];
139+
if (errorType) {
140+
throw {
141+
type: `Lambda:${errorType}`,
142+
message: result.data.errorMessage,
143+
};
144+
}
145+
// If the result of a lambda function is null or undefined, it returns as an empty string via HTTP.
146+
// Then, AppSync handles an empty string of the response as null (or undefined).
147+
if (result.data === '') {
148+
return null;
149+
}
150+
return result.data;
151+
} else {
152+
throw new Error(
153+
`Request failed with status code ${result.status}`,
154+
);
155+
}
130156
},
131157
};
132158
}

0 commit comments

Comments
 (0)