Madhu
Madhu

Reputation: 429

How to catch exception from lambda in state machine?

I am using state machines and raising custom error, but in my state machine I am not able to catch that exception.

Below is lambda snippet and state machine definition. Instead of going to catch block and error task.. Its throwing error at result selector attribute as below-

the JSONPath '$.Payload.tables' specified for the field 'tables.$' could not be found in the input

How I can ignore result selector attribute during exception?

My lambda code snippet -

        if schema is None:
            raise Exception("schema is not configured")

My statemachine -

      "ResultSelector": {
        "tables.$": "$.Payload.tables"
      },
     "ResultPath": "$.export_tables",
     "Catch": [
              {
                "ErrorEquals": [
                  "States.Runtime"
                ],
                 "ErrorEquals": [
            "States.ALL"
          ],
                "ResultPath": "$.error",
                "Next": "error state"
              }
            ],
      "Next": "Export Tables"
    },
    "error state": {
            "Type": "Fail"
          },
    "Export Tables": {
      "Type": "Map",
      "End": true,
      "ItemsPath": "$.export.tables",
      "Parameters": {
        "product.$": "$.product",
        "table_export_def.$": "$$.Map.Item.Value"
      },

Upvotes: 0

Views: 776

Answers (1)

Shreenitha
Shreenitha

Reputation: 349

You can catch custom errors by specifying ErrorEquals and Next attribute with fallback step as in aws docs

For example, if you want to catch runtime error :

"Catch": [ {
        "ErrorEquals": ["States.Runtime"],
        "Next": "CustomErrorFallback"
     },
     ...
      ]

Specify your custom fallback step to handle error with custom error message

"CustomErrorFallback": {
     "Type": "Pass",
     "Result": "schema is not configured",
     "End": true
  },

Upvotes: 1

Related Questions