0

我正在尝试构建一个带有循环(Map)的步进函数,只要抛出特定的错误就可以停止,就像这样

        "Job": {
          "Type": "Map",
          "InputPath": "$.content",
          "ItemsPath": "$.data",
          "MaxConcurrency": 0,
          "Iterator": {
            "StartAt": "Validate",
            "States": {
              "Validate": {
                "Type": "Task",
                "Resource": "arn:aws:lambda:us-east-1:123456789012:function:ship-val",
                "Catch": [
                    {
                      "ErrorEquals": [
                        "ErrorOne"
                      ],
                      "Next": "BreakLoop"
                    },
                    {
                      "ErrorEquals": ["States.ALL"],
                      "Next": "FailUncaughtError"
                    }
                ],
              },
              "FailUncaughtError":{
                "Type": "Fail",
                "Error": "Uncaught error"
              },
              "BreakLoop":{
                "Type": "Fail",
                "Error": "the loop should be stopped"
              }
            }
          },
          "ResultPath": "$.content.data",
          "End": true
        }

我试图将Next元素设置为CatchMap 之外的状态,但我做不到,因为 Map 只接受其中的状态。此外,AFAIK 在 AWS 文档中没有提到这样的功能

4

1 回答 1

0

与其在 Map 状态中捕获错误,不如不要捕获它并让 Map 状态失败。并向 M​​ap 状态添加一个 catch ,如果错误等于您要查找的错误,请继续下一步:

{
  "StartAt": "Map",
  "States": {
    "Map": {
      "Type": "Map",
      "ItemsPath": "$.array",
      "Iterator": {
        "StartAt": "FaultyLambda",
        "States": {
          "FaultyLambda": {
                  "Type": "Task",
                  "Resource": "arn:aws:states:::lambda:invoke",
                  "Parameters": {
                    "FunctionName": "your function arn",
                    "Payload": {
                      "a": 1
                    }
                  },
                  "End": true
                }
        }
      },
      "Catch": [
        {
          "ErrorEquals": ["ErrorOne"],
          "Next": "BreakLoop"
        }
      ],
      "Next": "BreakLoop"
    },
    "BreakLoop": {
      "Type": "Pass",
      "End": true
    }
  }
}

任何其他错误都不会被捕获并导致您的整个执行失败。

于 2021-09-29T06:24:42.450 回答