Class: RescueRegistry::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/rescue_registry/registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#ownerObject

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, **options)
  handler = handler_for_exception(exception)
  handler.formatted_response(content_type, **options)
end

#handler_for_exception(exception) ⇒ Object

Raises:



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_options = handler_info

  if handler_options[:status] == :passthrough
    handler_options = handler_options.merge(status: passthrough_status(exception))
  end

  handler_class.new(exception, **handler_options)
end

#handles_exception?(exception) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Raises:

  • (ArgumentError)


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, **options)
  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 = options[: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, options]
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)
  _, options = handler_info_for_exception(exception)
  return unless options

  if options[:status] == :passthrough
    passthrough ? passthrough_status(exception) : nil
  else
    options[:status]
  end
end