This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const formatErrorForApiGateway = ({ parsers } = { parsers: [] }) => | |
({ error }) => { | |
const { statusCode, key, message } = parseError(error, parsers); | |
throw `[${statusCode}] [${key}] ${message}`; | |
}; | |
... | |
const handler = middy(businessLogic) | |
.onError(formatErrorForApiGateway({ parsers: [parseAxiosError] }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
functions: | |
myLambda: | |
... | |
integration: lambda | |
response: ${file(./response.yml)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const formatErrorForApiGateway = ({ parsers } = { parsers: [] }) => | |
({ error }) => { | |
const { statusCode, key, message } = parseError(error, parsers); | |
throw JSON.stringify({ statusCode, key, message }); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
functions: | |
myLambda: | |
... | |
integration: lambda | |
response: | |
statusCodes: | |
200: | |
pattern: '' # Default response method | |
400: | |
pattern: '.*"statusCode":400.*' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parseError = (originalError, parsers) => { | |
let error = originalError; | |
while (!(error instanceof CustomError) && parsers.length) { | |
const { detect, parse } = parsers[0]; | |
if (detect(error)) { | |
error = parse(error); | |
} | |
parsers.shift(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parseStatusCode = (originalStatusCode) => { | |
switch (true) { | |
case defaultStatusCodes.has(originalStatusCode): | |
return originalStatusCode; | |
case originalStatusCode < 500: | |
return 400; | |
default: | |
return 500; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const defaultStatusCodes = new Set([400, 401, 403, 404, 422, 500, 502, 504]); | |
class CustomError extends Error { | |
constructor(statusCode: number, { key, message, payload }) { | |
super(message); | |
if (!defaultStatusCodes.has(statusCode)) { | |
throw new Error(`No integration response associated with statusCode ${statusCode}`); | |
} | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const formatErrorForApiGateway = ({ error }) => { | |
const { statusCode, key, message } = | |
error instanceof CustomError | |
? error | |
: new InternalServerError({ message: error.message }); | |
throw `[${statusCode}] [${key}] ${message}`; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createProxyHttpResponse = ({ statusCode, key, message }) => ({ | |
statusCode, | |
body: JSON.stringify({ key, message }), | |
headers: { 'Content-Type': 'application/json' }, | |
}); | |
const formatErrorForApiGateway = async ({ error, callback }) => | |
callback( | |
null, | |
createProxyHttpResponse( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const handler = async () => ({ | |
statusCode: 200, | |
body: JSON.stringify({ fruits: ['What a great tomato'] }), | |
headers: { 'Content-Type': 'application/json' }, | |
}); |
NewerOlder