Sha256: 5954d73479a95d7a50bd85eb4c9bd76bb328dfacf283dab69002b61f639edf3c

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

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

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

    handler    = Rescue.deprecate_positional_handler_signature(handler)
    handler    = Trailblazer::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::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] Rescue handlers have a new signature: (exception, *, &block)"
        handler.(exception, ctx, &block)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
trailblazer-macro-2.1.0.rc1 lib/trailblazer/operation/rescue.rb
trailblazer-macro-2.1.0.beta7 lib/trailblazer/operation/rescue.rb
trailblazer-macro-2.1.0.beta6 lib/trailblazer/operation/rescue.rb
trailblazer-macro-2.1.0.beta4 lib/trailblazer/operation/rescue.rb
trailblazer-macro-2.1.0.beta3 lib/trailblazer/operation/rescue.rb
trailblazer-macro-2.1.0.beta2 lib/trailblazer/operation/rescue.rb
trailblazer-macro-2.1.0.beta1 lib/trailblazer/operation/rescue.rb