Class: RescueRegistry::Registry
- Inherits:
-
Object
- Object
- RescueRegistry::Registry
- Defined in:
- lib/rescue_registry/registry.rb
Instance Attribute Summary collapse
-
#owner ⇒ Object
Returns the value of attribute owner.
Instance Method Summary collapse
- #build_response(content_type, exception, **options) ⇒ Object
- #handler_for_exception(exception) ⇒ Object
- #handles_exception?(exception) ⇒ Boolean
-
#initialize(owner) ⇒ Registry
constructor
A new instance of Registry.
- #initialize_dup(_other) ⇒ Object
- #passthrough_allowed? ⇒ Boolean
- #passthrough_status(exception) ⇒ Object
-
#register_exception(exception_class, handler: nil, **options) ⇒ Object
TODO: Support a shorthand for handler.
- #response_for_debugging(content_type, exception, traces: nil, fallback: :none) ⇒ Object
- #response_for_public(content_type, exception, fallback: :none) ⇒ Object
- #status_code_for_exception(exception, passthrough: true) ⇒ Object
Constructor Details
#initialize(owner) ⇒ Registry
Returns a new instance of Registry.
5 6 7 8 |
# File 'lib/rescue_registry/registry.rb', line 5 def initialize(owner) @owner = owner @handlers = { } end |
Instance Attribute Details
#owner ⇒ Object
Returns the value of attribute owner.
3 4 5 |
# File 'lib/rescue_registry/registry.rb', line 3 def owner @owner end |
Instance Method Details
#build_response(content_type, exception, **options) ⇒ Object
71 72 73 74 |
# File 'lib/rescue_registry/registry.rb', line 71 def build_response(content_type, exception, **) handler = handler_for_exception(exception) handler.formatted_response(content_type, **) end |
#handler_for_exception(exception) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rescue_registry/registry.rb', line 43 def handler_for_exception(exception) handler_info = handler_info_for_exception(exception) raise HandlerNotFound, "no handler found for #{exception.class}" unless handler_info handler_class, = handler_info if [:status] == :passthrough = .merge(status: passthrough_status(exception)) end handler_class.new(exception, **) end |
#handles_exception?(exception) ⇒ Boolean
56 57 58 |
# File 'lib/rescue_registry/registry.rb', line 56 def handles_exception?(exception) !handler_info_for_exception(exception).nil? end |
#initialize_dup(_other) ⇒ Object
10 11 12 |
# File 'lib/rescue_registry/registry.rb', line 10 def initialize_dup(_other) @handlers = @handlers.dup end |
#passthrough_allowed? ⇒ Boolean
14 15 16 |
# File 'lib/rescue_registry/registry.rb', line 14 def passthrough_allowed? defined?(ActionDispatch::ExceptionWrapper) end |
#passthrough_status(exception) ⇒ Object
18 19 20 |
# File 'lib/rescue_registry/registry.rb', line 18 def passthrough_status(exception) ::ActionDispatch::ExceptionWrapper.status_code_for_exception(exception.class.name) end |
#register_exception(exception_class, handler: nil, **options) ⇒ Object
TODO: Support a shorthand for handler
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rescue_registry/registry.rb', line 23 def register_exception(exception_class, handler: nil, **) raise ArgumentError, "#{exception_class} is not an Exception" unless exception_class <= Exception if owner.respond_to?(:default_exception_handler) handler ||= owner.default_exception_handler end raise ArgumentError, "handler must be provided" unless handler status = [:status] ||= handler.default_status raise ArgumentError, "status must be provided" unless status unless status.is_a?(Integer) || (passthrough_allowed? && status == :passthrough) raise ArgumentError, "invalid status: #{status}" end # TODO: Validate options here # We assign the status here as a default when looking up by class (and not instance) @handlers[exception_class] = [handler, ] end |
#response_for_debugging(content_type, exception, traces: nil, fallback: :none) ⇒ Object
76 77 78 |
# File 'lib/rescue_registry/registry.rb', line 76 def response_for_debugging(content_type, exception, traces: nil, fallback: :none) build_response(content_type, exception, show_details: true, traces: traces, fallback: fallback) end |
#response_for_public(content_type, exception, fallback: :none) ⇒ Object
80 81 82 |
# File 'lib/rescue_registry/registry.rb', line 80 def response_for_public(content_type, exception, fallback: :none) build_response(content_type, exception, fallback: fallback) end |
#status_code_for_exception(exception, passthrough: true) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rescue_registry/registry.rb', line 60 def status_code_for_exception(exception, passthrough: true) _, = handler_info_for_exception(exception) return unless if [:status] == :passthrough passthrough ? passthrough_status(exception) : nil else [:status] end end |