在 Node18x 运行时上运行的 AWS lambda 在 Node20x 运行时上运行时会引发不同的异常。
我在 AWS 状态机中使用了 lambda 函数。状态机对于 lambda 抛出的不同异常有重试机制。当我使用 Node18x 运行时作为 lambda 时,这是有效的。然而,由于异常格式会冒泡,它在 Node20x 运行时不起作用。
目标是将所有 lambda 迁移到最新支持的 AWS Node 运行时
这是阶跃函数 lambda 的示例:
"Send Request Upstream": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"OutputPath": "$.Payload",
"Parameters": {
"Payload.$": "$",
"FunctionName": "arn:aws:lambda:af-south-1:xxxxx:function:do-some-work"
},
"Retry": [{
"ErrorEquals": [
"Lambda.ServiceException",
"Lambda.AWSLambdaException",
"Lambda.SdkClientException",
"Lambda.TooManyRequestsException",
"InternalServerException",
"RateLimitException",
"TimeoutException"
],
"IntervalSeconds": 2,
"MaxAttempts": 10,
"BackoffRate": 2,
"JitterStrategy": "FULL"
},
{
"ErrorEquals": [
"UnreachableServerException",
"ServiceTemporarilyUnavailableException"
],
"IntervalSeconds": 5,
"MaxAttempts": 10,
"BackoffRate": 3,
"JitterStrategy": "FULL"
}
],
"Next": "DeleteMessage"
}
Node18x 运行时抛出的错误
{
"errorType": "UnreachableServerException",
"errorMessage": "connect ETIMEDOUT XX.XXX.XX.41:443",
"trace": [
"Error: ",
" at Object.<anonymous> (/XXXXXXXXXX/Exception.ts:43:40)",
" at Module._compile (node:internal/modules/cjs/loader:1356:14)",
" at Module.m._compile (/XXXXXXXXXX/node_modules/@pulumi/pulumi/vendor/[email protected]/index.js:3009:23)",
" at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)",
" at Object.require.extensions.<computed> [as .ts] (/XXXXXXXXXX/node_modules/@pulumi/pulumi/vendor/[email protected]/index.js:3011:12)",
" at Module.load (node:internal/modules/cjs/loader:1197:32)",
" at Function.Module._load (node:internal/modules/cjs/loader:1013:12)",
" at Module.require (node:internal/modules/cjs/loader:1225:19)",
" at require (node:internal/modules/helpers:177:18)",
" at Object.<anonymous> (/XXXXXXXXXX/utils.ts:4:21)",
" at Axios.request (/var/task/node_modules/axios/dist/node/axios.cjs:4224:41)",
" at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
" at async __f13.<anonymous> (/var/task/__index.js:464:26)",
" at async __f13.<anonymous> (/var/task/__index.js:498:27)",
" at async __f9.<anonymous> (/var/task/__index.js:582:26)",
" at async Runtime.<anonymous> (/var/task/__index.js:655:22)"
]
}
Node20x 运行时抛出的错误
{
"errorType": "object",
"errorMessage": "UnreachableServerException",
"trace": []
}
错误是如何抛出的
按照示例:https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-handling-error-conditions.html
function UnreachableServerException(name, message) {
this.name = name
this.message = message
}
UnreachableServerException.prototype = new Error()
throw new UnreachableServerException("UnreachableServerException", message)
我尝试过以下方法,但即使在 Node18 上也不起作用
class UnreachableServerException extends Error {
constructor(name, message) {
super(message)
this.name = name
}
}
throw new UnreachableServerException('UnreachableServerException', 'Unable to reach server")
您介意添加整个 Lambda 代码吗?我测试了以下内容:
export const handler = async (event) => {
UnreachableServerException.prototype = new Error()
throw new UnreachableServerException("UnreachableServerException", "my-test")
};
function UnreachableServerException(name, message) {
this.name = name
this.message = message
}
我明白了:
{
"errorType": "UnreachableServerException",
"errorMessage": "my-test",
"trace": [
"Error",
" at Runtime.handler (file:///var/task/index.mjs:2:43)",
" at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"
]
}