python - 如何从 Catch 状态的前一个函数访问 Step Function 中的错误信息

标签 python amazon-web-services aws-lambda state-machine aws-step-functions

我想知道如何访问我的 lambda 函数中引发的自定义异常的原因。我需要在 Step Functions 工作流程结束时访问它,如下所示。
下图是执行失败的示例。在 error-info 的输出中发现了错误( Error 对象,带有它自己的 CauseParseTextractOutput 部分) ,但我想知道如何在 OutputNotFound 中访问它如下所示。
阶跃函数图
enter image description here
输出ParseTextractOutput的输出是

{
  "event"...
  "error-info": {
    "Error": "OutputNotFoundException",
    "Cause": "{\"errorMessage\": \"Contents of Textracted file: {...}}"
    }
  }
}
我想在这些字段(Step Functions 定义)中以某种方式访问​​这些数据:
...
  "States": {
    "OutputNotFound": {
      "Type": "Fail",
      "Error": "<useful stuff here, like $.error-info.Error or something>",
      "Cause": "<useful stuff here, like $.error-info.Cause or something>"
    },
...
    "ParseTextractOutput": {
      "Type": "Task",
      "Resource": "functionARN",
      "Catch": [
        {
          "ErrorEquals": ["OutputNotFoundException"],
          "ResultPath": "$.error-info",
          "Next": "OutputNotFound"
        }
      ],
      "End": true
    }
Python代码
这是函数 ParseTextractOutput 的相关代码.
class OutputNotFoundException(Exception):
  pass

...

try:
  blocks = data['Blocks']
except KeyError as e:
  raise OutputNotFoundException('Contents of Textracted file: {}'.format(data))

最佳答案

目前(当前版本为 https://states-language.net/spec.html )Fail.ErrorFail.Cause不能是动态的。传递给 Fail 的输入状态被忽略,修复字符串用于错误和原因。
我们可以查看Fail作为执行中宣布修复消息的一个点,用错误指示执行结束并退出。
这意味着必须在这些公告点之前进行任何处理。正如@frosty 在评论中提到的 Choice状态很有用。
备选方案 1:使用选择
下面是一个例子:
State machine with choice before fail states
假设我的 Lambda 函数中有这个 Python 代码:

class OutputNotFoundException(Exception):
  pass



def lambda_handler(event, context):
    raise OutputNotFoundException('Error message A')
当函数返回时,输出将是这样的 JSON:
{
  "Error": "OutputNotFoundException",
  "Cause": "{\"errorMessage\":\"Error message A\",\"errorType\":\"OutputNotFoundException\",\"stackTrace\":[\"...\\n\"]}"
}
请注意“原因”是另一个字符串编码的 JSON。我们可以转换 OutputNotFoundPass并使用内在函数 StringToJson()将编码的字符串转换为普通的 JSON 以便以后更容易处理:
    "OutputNotFound": {
      "Type": "Pass",
      "Parameters": {
        "details.$": "States.StringToJson($.Cause)"
      },
      "Next": "Error message?"
    },
现在我们有这样的输出:
{
  "details": {
    "errorMessage": "Error message A",
    "errorType": "OutputNotFoundException",
    "stackTrace": ["...\n"]
  }
}
下一个状态将是 Choice其中查看 $.details.errorMessage决定适当的失败状态:
    "Error message?": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.details.errorMessage",
          "StringEquals": "Error message A",
          "Next": "Error A"
        },
        {
          "Variable": "$.details.errorMessage",
          "StringEquals": "Error message B",
          "Next": "Error B"
        }
      ],
      "Default": "Unknown Error"
    },
现在每个选择都指向一个正常的 Fail声明修复字符串的状态:
    "Error A": {
      "Type": "Fail",
      "Error": "OutputNotFoundException",
      "Cause": "OutputNotFoundException of type A happened"
    },
选择 2:以通过结束
如果您的目的是将确切的错误消息作为执行的输出以供以后记录/处理,那么一种方法可以留在 Pass状态:
    "OutputNotFound": {
      "Type": "Pass",
      "Parameters": {
        "details.$": "States.StringToJson($.Cause)",
        "isError": true
      },
      "End": true
    }
当然,这个解决方案的缺点是,执行以成功状态结束,我们需要处理输出以发现有错误(因此上面的额外 isError 字段)

关于python - 如何从 Catch 状态的前一个函数访问 Step Function 中的错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56240759/

相关文章:

amazon-web-services - 如何命名使用 cloudformation 模板创建的自定义托管策略

amazon-web-services - 如何终止/终止正在运行的AWS Lambda函数?

python - Pyspark 中的随机样本没有重复

python - 为什么 python imghdr 测试函数将文件作为参数?

amazon-web-services - 将AWS CloudFormation转换为Terraform模板

amazon-web-services - AWS s3 CLI "cp"和 "sync"触发什么事件?

php - 如何使用 AWS SDK PHP 将触发器添加到 AWS Lambda 函数?

python - pyqt5主窗口内弹出自定义对话框输入

python - 如何使用 Pygame 执行基于时间的音频?

amazon-web-services - 如何处理 AWS Glue 中的 `No enum constant` 错误