Sha256: 84f8d42db32f8911442828cae57b8e35e397cdd90dddcef101327e72135d1f66

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

class Trailblazer::V2_1::Operation
  NoopHandler = lambda { |*| }

  def self.Rescue(*exceptions, handler: NoopHandler, &block)
    exceptions = [StandardError] unless exceptions.any?

    handler    = Rescue.deprecate_positional_handler_signature(handler)
    handler    = Trailblazer::V2_1::Option(handler)

    # This block is evaluated by {Wrap}.
    rescue_block = ->((ctx, flow_options), **circuit_options, &nested_activity) do
      begin
        nested_activity.call
      rescue *exceptions => exception
        # DISCUSS: should we deprecate this signature and rather apply the Task API here?
        handler.call(exception, ctx, **circuit_options) # FIXME: when there's an error here, it shows the wrong exception!

        [ Trailblazer::V2_1::Operation::Railway.fail!, [ctx, flow_options] ]
      end
    end

    Wrap( rescue_block, id: "Rescue(#{rand(100)})", &block )
    # FIXME: name
    # [ step, name: "Rescue:#{block.source_location.last}" ]
  end

  # TODO: remove me in 2.2.
  module Rescue
    def self.deprecate_positional_handler_signature(handler)
      return handler if handler.is_a?(Symbol) # can't do nutting about this.

      arity = handler.is_a?(Class) ? handler.method(:call).arity : handler.arity

      return handler if arity != 2 # means (exception, (ctx, flow_options), *, &block), "new style"

      ->(exception, (ctx, flow_options), **circuit_options, &block) do
        warn "[Trailblazer::V2_1] Rescue handlers have a new signature: (exception, *, &block)"
        handler.(exception, ctx, &block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trailblazer-future-2.1.0.rc1 lib/trailblazer/v2_1/operation/rescue.rb