Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Step Function Lambda error catch clause custom error with new fields

The setup is an AWS Step Function with Lambdas which throw errors in a catch clause and should add them to the event payload for an Error Handler Lambda at the end of the chain. This is accomplished with adding a result path like

"Catch": [ {
  "ErrorEquals": [ "States.ALL" ],
  "ResultPath": "$.error-info",
  "Next": "Error Handler"
}]

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

I now need to create custom errors that include new fields lets say a new field called "lambdaName".

For that I would customize an error class like this:

class SFLambdaError extends Error {
    constructor(message,lambdaName){
        super(message)
        this.lambdaName = lambdaName
    }
}

Testing the Lambda directly this outputs the desired new field and looks fine:

Invoke Error    {"errorType":"SFLambdaError","errorMessage":"someNumber.replace is not a function","lambdaName":"testLambdaName","stack": (...)}

But when implemented in the flow of a Step Function and outputed into "error-info" of the event, the new fields got cut of like following:

"error-info": {
    "Error": "SFLambdaError",
    "Cause": "{\"errorType\":\"SFLambdaError\",\"errorMessage\":\"someNumber.replace is not a function\",\"trace\":[\"SFLambdaError: someNumber.replace is not a function\",\"    at Runtime.exports.handler (/var/task/index.js:27:23)\",\"    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)\"]}"

So it seems that SF error result path just allowes for the standard error class fields to be given out.

Work arounds I tested are to pass all desired fields in errorMessage via stringifying and parsing at the other end in the error handler. But I hope there must be a better way.

So my question: is there a thing I've overseen in the docs or a cleaner workaroun?

Thanks for any help!

like image 245
mafehx Avatar asked Apr 09 '26 22:04

mafehx


1 Answers

Unfortunately there is no handy way to handle such case. A workaround is using an extra state with builtin intrinsic function States.StringToJson:


"Catch": [ {
  "ErrorEquals": [ "States.ALL" ],
  "ResultPath": "$.error-info",
  "Next": "Error Handler Preprocessing"
}]

...

"Error Handler Preprocessing": {
  "Type": "Pass",
  "Parameters": {
    "Error.$": "$.error-info.Error",
    "Cause.$": "States.StringToJson($.error-info.Cause)"
  },
  "ResultPath": "$.error-info",
  "Next": "Error Handler"
},

"Error Handler": {
  "Type": "Task",
  ...
}

References

  • https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-intrinsic-functions.html
like image 199
chehsunliu Avatar answered Apr 12 '26 15:04

chehsunliu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!