Sha256: 9f5b5d76ced2dd3318e82eeaa492f5ea37e7f81b199fdb80b6742f6fc6efd457

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module Trailblazer
  module Macro
    NoopHandler = lambda { |*| }

    def self.Rescue(*exceptions, handler: NoopHandler, id: Rescue.random_id, &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: id, &block)
    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 nothing about this.
        return handler if handler.method(:call).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

      # TODO: for legacy reasons, we pass `:id` to {#id_for}. In 2.2, remove the id hint and use
      # generic {Macro.id_for} behavior.
      def self.random_id
        Macro.id_for(nil, macro: :Rescue, id: rand(1000))
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trailblazer-macro-2.1.16 lib/trailblazer/macro/rescue.rb