简体   繁体   English

如何使用 AWS Step Functions 将失败任务的输入和错误传递给回退任务?

[英]How can I pass the input and error of my failing task to a fallback task using AWS Step Functions?

I'm trying to add some error handling to my step functions state machine and I've been having issues on a specific scenario, for example if I have a fail on a step I have a catch to send it to another function, but in that function I need to perform logic using the last step input data (the one that failed), however, I just receive an error, is there a way to pass the input data with the error as well?我正在尝试向我的步骤函数状态机添加一些错误处理,并且我在特定场景中遇到了问题,例如,如果我在某个步骤上失败了,我可以将其发送到另一个函数,但是在该功能我需要使用最后一步输入数据(失败的那个)来执行逻辑,但是,我只是收到一个错误,有没有办法传递带有错误的输入数据?

Send Notification:
        Type: Task
        Resource: !GetAtt CommunicationSendCertifiedEmail.Arn
        Retry:
          - ErrorEquals:
            - TierOneError
            IntervalSeconds: 2
            MaxAttempts: 9
            BackoffRate: 1.5
          - ErrorEquals:
            - TierTwoError
            IntervalSeconds: 4
            MaxAttempts: 6
            BackoffRate: 1.5
          - ErrorEquals:
            - TierThreeError
            IntervalSeconds: 4
            MaxAttempts: 3
            BackoffRate: 2
          - ErrorEquals:
            - States.Timeout
            - States.Runtime
            - States.TaskFailed
            - Lambda.ServiceException
            IntervalSeconds: 4
            MaxAttempts: 3
            BackoffRate: 2
        Catch:
          - ErrorEquals:
            - States.ALL
            Next: Send Email Notification

on that "Send email notification" is where I need the input before the execution failed, but as I stated before I just get something like this:在“发送电子邮件通知”上是我在执行失败之前需要输入的地方,但正如我之前所说,我只是得到这样的东西:

{
  "Error": "Error",
  "Cause": "{\"errorType\":\"Error\",\"errorMessage\":...
}

I would like to get something like this:我想得到这样的东西:

{
  "data": "{...}",
  "Error": "Error",
  "Cause": "{\"errorType\":\"Error\",\"errorMessage\":...
}

Any ideas?有任何想法吗? Is it even possible?甚至有可能吗?

Use ResultPath in a Catch to include the error with the original input, instead of replacing it.Catch使用ResultPath将错误包含在原始输入中,而不是替换它。

ResultPath (Optional)结果路径(可选)

A path that determines what input is sent to the state specified in the Next field.确定将哪些输入发送到 Next 字段中指定的状态的路径。


If you don't specify the ResultPath field, it defaults to $ . 如果未指定ResultPath字段,则默认为$

This selects and overwrites the entire input to replace it with the state output (which is an error in this case), which is why you're not getting any input data coming through.这将选择并覆盖整个输入以将其替换为状态输出(在这种情况下是错误的),这就是为什么您没有获得任何输入数据的原因。

Here's an example - this is not what you want but is what is currently happening :这是一个示例 -这不是您想要的,而是当前正在发生的事情

在此处输入图片说明


Specify a ResultPath for where the error output should go.为错误输出应该去的位置指定一个ResultPath

That way, it'll take the state output (which is an error in this case) and includes it with the input.这样一来,它会采取的状态输出(这是一个错误在这种情况下),并包括它与输入。

This won't overwrite all of the state input, which allows you to preserve the input.这不会覆盖所有状态输入,这允许您保留输入。

Here's an example - this is what you want :这是一个例子 -这就是你想要的

在此处输入图片说明


Try:尝试:

Catch:
  - ErrorEquals:
    - States.ALL
    Next: Send Email Notification
    ResultPath: $.error

If the previous Catch statement catches an error, it'll include the error output in an error node within the state input (the node is called error as we specified the path to be $.error ).如果前面的Catch声明捕获了一个错误,它会包括一个错误输出error状态下输入节点(节点被称为error ,因为我们指定的路径是$.error )。

This acts like the second figure above.这就像上面的第二个图。


For example, if the state input for Send Notification is:例如,如果发送通知的状态输入是:

{
    "foo": "bar"
}

The state output that is sent to Send Email Notification , when catching an error, will be the following.当捕获错误时,发送到Send Email Notification的状态输出将如下所示。

{
  "foo": "bar",
  "error": {
    "Error": "Error here"
  }
}

It may also be the below, as the object also usually contains the Cause field (as it does in your case).它也可能是下面的,因为对象通常还包含Cause字段(就像你的情况一样)。 This field's value is a human-readable description of the error.该字段的值是人类可读的错误描述。

{
  "foo": "bar",
  "error": {
    "Error": "Error here",
    "Cause": "{\"errorType\":\"Error\",\"errorMessage\"
  }
}

Important note:重要的提示:

Choose a unique name for the error node depending on your input data as I've just used error for demonstration purposes.根据您的输入数据为error节点选择一个唯一的名称,因为我刚刚将error用于演示目的。

If the node specified already exists eg you already have a field with the name error , the ResultPath will update & overwrites the node with your error output which isn't what you want.如果指定的节点已经存在,例如您已经有一个名称为error的字段,则ResultPath将使用您不想要的错误输出更新并覆盖该节点。

A picture is worth a thousand words, so this is the 3rd path that is possible using a ResultPath (again, this is not what you want ):一张图片值一千字,所以这是使用ResultPath可能的第三条路径(同样,这不是你想要的):

在此处输入图片说明


If you're curious, the last path possible (which isn't related to the question but good to know) is discarding the state result (which you need, hence why it isn't related) and keeping the original input.如果您好奇,最后一条可能的路径(与问题无关但很高兴知道)是丢弃状态结果(您需要它,因此它不相关)并保留原始输入。

Setting ResultPath to null achieves this, as outlined here .设置ResultPathnull实现这一点,因为概述这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM