Sha256: ca11d845ca072d025bad23b0c0d66822d7b5ae14919e44553432885ce0b8fc10

Contents?: true

Size: 930 Bytes

Versions: 5

Compression:

Stored size: 930 Bytes

Contents

module BloodContracts::Core
  # Concern to wrap matching process with exception handling
  #
  # @example Defines a type with automatic exception handling in form of types
  #   class JsonType < ::BC::Refined
  #     prepend ExceptionHandling
  #
  #     def match
  #       @context[:parsed_json] = JSON.parse(value)
  #       self
  #     end
  #   end
  #
  module ExceptionHandling
    # Runs the matching process and returns an ExceptionCaught if
    # StandardError happened inside match call
    #
    # @return [Refined]
    #
    def match
      super
    rescue StandardError => ex
      exception(ex)
    end

    # Wraps the exception in refinement type
    #
    # @param exc [Exception] raised exception
    # @option context [Hash] shared context of matching pipeline
    # @return [ExceptionCaught]
    #
    def exception(exc, context: @context)
      ExceptionCaught.new(exc, context: context)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
blood_contracts-ext-0.1.4 lib/blood_contracts/core/exception_handling.rb
blood_contracts-ext-0.1.3 lib/blood_contracts/core/exception_handling.rb
blood_contracts-ext-0.1.2 lib/blood_contracts/core/exception_handling.rb
blood_contracts-ext-0.1.1 lib/blood_contracts/core/exception_handling.rb
blood_contracts-ext-0.1.0 lib/blood_contracts/core/exception_handling.rb