Error handling in Step Functions

To Nha Notes | Nov. 28, 2023, 10:30 a.m.

 

Create a Lambda function that fails

exports.handler = async (event, context) => {
    function CustomError(message) {
        this.name = 'CustomError';
        this.message = message;
    }
    CustomError.prototype = new Error();

    throw new CustomError('This is a custom error!');
};

The following is an example ARN:

arn:aws:lambda:us-east-1:123456789012:function:FailFunction

Create a state machine with a Catch field

{
   "Comment": "A Catch example of the Amazon States Language using an AWS Lambda function",
   "StartAt": "CreateAccount",
   "States": {
      "CreateAccount": {
         "Type": "Task",
         "Resource": "arn:aws:lambda:us-east-1:123456789012:function:FailFunction",
         "Catch": [ {
            "ErrorEquals": ["CustomError"],
            "Next": "CustomErrorFallback"
         }, {
            "ErrorEquals": ["States.TaskFailed"],
            "Next": "ReservedTypeFallback"
         }, {
            "ErrorEquals": ["States.ALL"],
            "Next": "CatchAllFallback"
         } ],
         "End": true
      },
      "CustomErrorFallback": {
         "Type": "Pass",
         "Result": "This is a fallback from a custom Lambda function exception",
         "End": true
      },
      "ReservedTypeFallback": {
         "Type": "Pass",
         "Result": "This is a fallback from a reserved error code",
         "End": true
      },
      "CatchAllFallback": {
         "Type": "Pass",
         "Result": "This is a fallback from any error code",
         "End": true
      }
   }
}

References

https://docs.aws.amazon.com/step-functions/latest/dg/concepts-error-handling.html

https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-handling-error-conditions.html