Module: BloodContracts::Core::ExceptionHandling

Included in:
BloodContracts::Core::Ext::Refined
Defined in:
lib/blood_contracts/core/exception_handling.rb

Overview

Concern to wrap matching process with exception handling

Examples:

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

Instance Method Summary collapse

Instance Method Details

#exception(exc, context: @context) ⇒ ExceptionCaught

Wraps the exception in refinement type

Parameters:

  • exc (Exception)

    raised exception

  • context (Hash) (defaults to: @context)

    a customizable set of options

Options Hash (context:):

  • shared (Hash)

    context of matching pipeline

Returns:



32
33
34
# File 'lib/blood_contracts/core/exception_handling.rb', line 32

def exception(exc, context: @context)
  ExceptionCaught.new(exc, context: context)
end

#matchRefined

Runs the matching process and returns an ExceptionCaught if StandardError happened inside match call

Returns:

  • (Refined)


20
21
22
23
24
# File 'lib/blood_contracts/core/exception_handling.rb', line 20

def match
  super
rescue StandardError => ex
  exception(ex)
end