Class: ExceptionTransformer::Transformer
- Inherits:
-
Object
- Object
- ExceptionTransformer::Transformer
- Defined in:
- lib/exception_transformer/transformer.rb
Constant Summary collapse
- MAX_MESSAGE_SIZE =
100
Instance Attribute Summary collapse
-
#delegate ⇒ Object
Returns the value of attribute delegate.
-
#mappings ⇒ Object
Returns the value of attribute mappings.
-
#strategy ⇒ Object
Returns the value of attribute strategy.
-
#validator ⇒ Object
Returns the value of attribute validator.
Instance Method Summary collapse
- #after_rescue(obj, e, calling_method, except: [], use_default: true, opts: {}) ⇒ Object
- #after_yield(obj, result, calling_method, except: [], use_default: true, opts: {}) ⇒ Object
-
#initialize(strategy) ⇒ Transformer
constructor
A new instance of Transformer.
- #register_target(target, exceptions) ⇒ Object
Constructor Details
#initialize(strategy) ⇒ Transformer
Returns a new instance of Transformer.
8 9 10 11 |
# File 'lib/exception_transformer/transformer.rb', line 8 def initialize(strategy) self.strategy = strategy self.mappings = {} end |
Instance Attribute Details
#delegate ⇒ Object
Returns the value of attribute delegate.
4 5 6 |
# File 'lib/exception_transformer/transformer.rb', line 4 def delegate @delegate end |
#mappings ⇒ Object
Returns the value of attribute mappings.
4 5 6 |
# File 'lib/exception_transformer/transformer.rb', line 4 def mappings @mappings end |
#strategy ⇒ Object
Returns the value of attribute strategy.
4 5 6 |
# File 'lib/exception_transformer/transformer.rb', line 4 def strategy @strategy end |
#validator ⇒ Object
Returns the value of attribute validator.
4 5 6 |
# File 'lib/exception_transformer/transformer.rb', line 4 def validator @validator end |
Instance Method Details
#after_rescue(obj, e, calling_method, except: [], use_default: true, opts: {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/exception_transformer/transformer.rb', line 26 def after_rescue(obj, e, calling_method, except: [], use_default: true, opts: {}) with_reporting do case strategy when :delegate obj.instance_exec(e, calling_method, opts, &delegate) when :rewrite, :regex exception, = find_target(e, except, use_default) end if exception.present? raise exception, .first(MAX_MESSAGE_SIZE), e.backtrace else # Couldn't transform the exception to a defined mapping. raise e end end end |
#after_yield(obj, result, calling_method, except: [], use_default: true, opts: {}) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/exception_transformer/transformer.rb', line 44 def after_yield(obj, result, calling_method, except: [], use_default: true, opts: {}) with_reporting do case strategy when :validate obj.instance_exec(result, calling_method, opts, &validator) end end end |
#register_target(target, exceptions) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/exception_transformer/transformer.rb', line 13 def register_target(target, exceptions) case strategy when :validate self.validator = target when :delegate self.delegate = target when :rewrite, :regex exceptions.each do |klass| self.mappings[klass] = target end end end |