【问题标题】:Passing data to step function catch将数据传递给 step 函数 catch
【发布时间】:2019-08-03 09:26:13
【问题描述】:

我在 AWS 上使用步进函数。考虑由 lambda 组成的状态机:

"StartAt": "Metadata",
              "States": {
                    "Metadata": {
                    "Type": "Task",
                    "Resource": "${metadataArn}",
                    "Next": "StoreMetadata",
                    "Retry" : [
                            {
                                "ErrorEquals": [ "States.All" ],
                                "IntervalSeconds": 2,
                                "MaxAttempts": 3
                            }
                    ],
                    "Catch": [
                        {
                            "ErrorEquals": [ "States.All" ],
                            "Next": "ErrorHandler"
                        }
                    ]
                  } ...
                      ...

如何将特定数据传递给“ErrorHandler”。例如,失败的步骤,可能是一条数据。我正在使用 nodejs,但可以推断到任何运行时。

例如,在节点中,我们可能有一个 lambda,其中:

module.exports.handler = async input => {
  const ids = JSON.parse(input).ids
  // try to read in data for ids.
  // read fails / throws exception
}

如何让错误处理程序获取 id 数组以便我可以将它们标记为失败?如果这个“ErrorHandler”是多个步骤的问题,我怎么知道哪些步骤失败了?

【问题讨论】:

    标签: amazon-web-services aws-lambda aws-step-functions


    【解决方案1】:

    我找到了答案,您可以使用ResultPath 将原始输入与错误一起传递。我想我会将步骤作为属性包含在所有输入中,以便我可以知道哪个步骤失败了。有关说明,请参阅 docs。基本上要实现这一点,您只需添加 ResultPath 属性,如下所示:

    "Catch": [
      {
        "ErrorEquals": [ "States.All" ],
        "Next": "ErrorHandler"
        "ResultPath": "$.error"
      }
    ]
    

    【讨论】:

    • 你是救生员!
    • 您只需将我从 4 小时的调试中拯救出来!巨大的道具!
    【解决方案2】:

    我想在@Zachscs 的答案中补充一点,在"Type": "Map" 上执行"Catch" 时需要小心,因为这是在执行“ResultPath”:“$.error”时,会抛出:

    无法将步骤“错误”应用于输入 [...]

    这是有道理的,因为输入是一个数组。您可以通过向错误添加一个简单的从零开始的索引来解决它,如下所示:

    "Type": "Map",
    "Next": "Finish",
    "Catch": [
      {
        "ErrorEquals": [ "States.All" ],
        "Next": "ErrorHandler",
        "Comment": "Note the $[0] down below",
        "ResultPath": "$[0].error"
      }
    ]
    

    这会将它附加到数组$[1].error的第二个索引

    【讨论】:

      【解决方案3】:

      对于那些使用 AWS CDK 创建步进函数的人:

      yourTask.addCatch(sendFailureNotify, {
        resultPath: '$.error'
      });
      

      sendFailureNotify 是你的 lambda。而道具 resultPath? 是您设置 $.error

      的位置
      /**
       * Error handler details.
       */
      export interface CatchProps {
          /**
           * Errors to recover from by going to the given state.
           *
           * A list of error strings to retry, which can be either predefined errors
           * (for example Errors.NoChoiceMatched) or a self-defined error.
           *
           * @default All errors
           */
          readonly errors?: string[];
          /**
           * JSONPath expression to indicate where to inject the error data.
           *
           * May also be the special value DISCARD, which will cause the error
           * data to be discarded.
           *
           * @default $
           */
          readonly resultPath?: string;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-14
        • 2021-02-27
        • 1970-01-01
        • 2022-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多