AlexanderMint/upment-hanami

View on GitHub
lib/upment/trailblazer/operation/rescue.rb

Summary

Maintainability
A
0 mins
Test Coverage
# frozen_string_literal: true

# Cloned: https://github.com/trailblazer/trailblazer/blob/master/lib/trailblazer/operation/rescue.rb
# rubocop:disable all
class Trailblazer::Operation
  def self.Rescue(*exceptions, handler: lambda { |*| }, &block)
    exceptions = [StandardError] unless exceptions.any?
    handler    = Option.(handler)

    rescue_block = ->(options, operation, *, &nested_pipe) {
      begin
        res = nested_pipe.call
        res.first == ::Pipetree::Railway::Right # FIXME.
      rescue *exceptions => exception
        handler.call(operation, exception, options)
        false
      end
    }

    step, _ = Wrap(rescue_block, &block)

    [ step, name: "Rescue:#{block.source_location.last}" ]
  end
end