module ExceptionTransformer::ClassMethods

Public Instance Methods

find_exception_transformer(group) click to toggle source
# File lib/exception_transformer.rb, line 68
def find_exception_transformer(group)
  exception_transformers[group]
end
find_or_create_exception_transformer(group, strategy) click to toggle source
# File lib/exception_transformer.rb, line 72
def find_or_create_exception_transformer(group, strategy)
  exception_transformers[group] ||= Transformer.new(strategy)
end
transform_exceptions(*exceptions, group: :default, to: nil, where: nil, with: nil, validate: nil) click to toggle source

Add exceptions to be transformed in `handle_exceptions` block. @examples

1. Transform several errors to a single error:

     transform_exceptions FooError, BazError, to: BarError

2. Transform a single error based on it's message:

     transform_exceptions FooError, where: {
       /Invalid API key/i => BarError,
       :default => RuntimeError
     }

   To prevent *all* errors being caught via the `:default` branch,
   pass `use_default: false` to `handle_exceptions`.

3. Validate a response with a Proc that takes two parameters. The
   first parameter is the response, and the second is the calling method.

      transform_exceptions validate: proc { |response, action| ... }

4. Inspect an error with a Proc that takes two parameters. The
   first parameter is the error, and the second is the calling method.

      transform_exceptions with: proc { |err, action| ... }
# File lib/exception_transformer.rb, line 58
def transform_exceptions(*exceptions, group: :default, to: nil, where: nil, with: nil, validate: nil)
  strategies = { validate: validate, delegate: with, rewrite: to, regex: where }

  strategy = strategies.keys.find { |s| strategies[s].present? }
  target   = strategies[strategy]

  transformer = find_or_create_exception_transformer(group, strategy)
  transformer.register_target(target, exceptions)
end

Private Instance Methods

exception_transformers() click to toggle source
# File lib/exception_transformer.rb, line 78
def exception_transformers
  @exception_transformers ||= {}
end