Handling Errors

The way you handle errors in AWS Flow Framework for Ruby depends on whether the workflow is synchronous or asynchronous.

Errors in Synchronous Workflows#

If your activities or workflows are synchronous, you can use Ruby's standard begin/rescue/ensure pattern to handle errors. For example:

# from within a decider
begin
  my_activity_client.my_activity
rescue ActivityTaskTimedOutException => e
  # handle timeout
rescue ActivityTaskFailedException => e
  # handle failure
ensure
  # clean up and stuff
end

Tip

AWS Flow Framework for Ruby exception names are based on the event types specified in HistoryEvent in the Amazon Simple Workflow Service API Reference. You can predict the exception name by adding "Exception" to the end of the event type. For example, an exception in TimerFired will result in a TimerFiredException.

Errors in Asynchronous Workflows#

If your activities or workflows are asynchronous, (using send_async), use the Core#error_handler core method, which is modeled after Ruby's begin/rescue/ensure pattern. Here is an example of its use:

error_handler do |t|
  t.begin do
    my_activity_client.send_async :my_activity
  end
  t.rescue ActivityTaskTimedOutException do |e|
    # handle timeout
  end
  t.rescue ActivityTaskFailedException do |e|
    # handle failure
  end
  t.ensure do
    # clean up and stuff
  end
end

You pass the error_handler method a block that takes a single argument (a Core::BeginRescueEnsure object). The BeginRescueEnsure class has three methods: begin, rescue, and ensure, which take parts of your error handling logic.

For more information about writing code for asynchronous tasks, see Executing Tasks Asynchronously.

Additional Error Handling Examples#

The handle_error recipe in the public aws-flow-ruby-samples project on GitHub provides a number of examples of handling errors for both synchronous and asynchronous tasks.