Sha256: a481e81d051fc8b2d224b715c6a770454c5a0817ff324b3b66217c69924d1b3f

Contents?: true

Size: 1.57 KB

Versions: 5

Compression:

Stored size: 1.57 KB

Contents

module Trailblazer
  module Macro
    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!

          [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
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
trailblazer-macro-2.1.0 lib/trailblazer/macro/rescue.rb
trailblazer-macro-2.1.0.rc14 lib/trailblazer/macro/rescue.rb
trailblazer-macro-2.1.0.rc13 lib/trailblazer/macro/rescue.rb
trailblazer-macro-2.1.0.rc12 lib/trailblazer/macro/rescue.rb
trailblazer-macro-2.1.0.rc11 lib/trailblazer/macro/rescue.rb