Module: OpenStax::RescueFrom

Defined in:
lib/openstax/rescue_from.rb,
lib/openstax/rescue_from/engine.rb,
lib/openstax/rescue_from/logger.rb,
lib/openstax/rescue_from/version.rb,
lib/openstax/rescue_from/controller.rb,
lib/openstax/rescue_from/view_helpers.rb,
lib/openstax/rescue_from/configuration.rb,
lib/openstax/rescue_from/mute_listener.rb,
lib/openstax/rescue_from/background_job.rb,
lib/openstax/rescue_from/exception_proxy.rb,
lib/openstax/rescue_from/exception_options.rb,
lib/openstax/rescue_from/default_exceptions.rb,
lib/openstax/rescue_from/exception_cause_proxy.rb

Defined Under Namespace

Modules: BackgroundJob, Controller, ViewHelpers Classes: Configuration, DefaultExceptions, Engine, ExceptionCauseProxy, ExceptionOptions, ExceptionProxy, Logger, MuteListener

Constant Summary collapse

VERSION =
'4.0.2'

Class Method Summary collapse

Class Method Details

.configurationObject



114
115
116
# File 'lib/openstax/rescue_from.rb', line 114

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



110
111
112
# File 'lib/openstax/rescue_from.rb', line 110

def configure
  yield configuration
end

.do_not_reraiseObject

Not threadsafe



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/openstax/rescue_from.rb', line 27

def do_not_reraise
  original = configuration.raise_exceptions
  original_background = configuration.raise_background_exceptions
  begin
    configuration.raise_exceptions = false
    configuration.raise_background_exceptions = false
    yield
  ensure
    configuration.raise_exceptions = original
    configuration.raise_background_exceptions = original_background
  end
end

.extras_proc(exception_name) ⇒ Object



102
103
104
# File 'lib/openstax/rescue_from.rb', line 102

def extras_proc(exception_name)
  @@registered_exceptions[exception_name].extras
end

.friendly_message(proxy) ⇒ Object



80
81
82
83
84
# File 'lib/openstax/rescue_from.rb', line 80

def friendly_message(proxy)
  options_for(proxy.name).message ||
    friendly_status_messages[proxy.status] ||
      default_friendly_message
end

.generate_idObject



106
107
108
# File 'lib/openstax/rescue_from.rb', line 106

def generate_id
  sprintf "%06d", "#{SecureRandom.random_number(10**6)}"
end

.http_code(status) ⇒ Object



98
99
100
# File 'lib/openstax/rescue_from.rb', line 98

def http_code(status)
  Rack::Utils.status_code(status)
end

.non_notifying_exceptionsObject



72
73
74
# File 'lib/openstax/rescue_from.rb', line 72

def non_notifying_exceptions
  @@registered_exceptions.reject { |_, v| v.notify? }.keys
end

.notifies_for?(exception_name) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/openstax/rescue_from.rb', line 86

def notifies_for?(exception_name)
  notifying_exceptions.include?(exception_name)
end

.notifying_exceptionsObject



76
77
78
# File 'lib/openstax/rescue_from.rb', line 76

def notifying_exceptions
  @@registered_exceptions.select { |_, v| v.notify? }.keys
end

.perform_background_rescue(exception, listener = MuteListener.new) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/openstax/rescue_from.rb', line 18

def perform_background_rescue(exception, listener = MuteListener.new)
  proxy = ExceptionProxy.new(exception)
  register_unrecognized_exception(proxy.name)
  log_background_system_error(proxy)
  send_notifying_background_exceptions(proxy)
  finish_background_exception_rescue(proxy, listener)
end

.perform_rescue(exception, listener = MuteListener.new) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/openstax/rescue_from.rb', line 10

def perform_rescue(exception, listener = MuteListener.new)
  proxy = ExceptionProxy.new(exception)
  register_unrecognized_exception(proxy.name)
  log_system_error(proxy)
  send_notifying_exceptions(proxy, listener)
  finish_exception_rescue(proxy, listener)
end

.register_exception(exception, options = {}) ⇒ Object



40
41
42
43
44
45
# File 'lib/openstax/rescue_from.rb', line 40

def register_exception(exception, options = {})
  name = exception.is_a?(String) ? exception : exception.name
  options = ExceptionOptions.new(options)
  @@registered_exceptions ||= {}
  @@registered_exceptions[name] = options
end

.register_unrecognized_exception(exception_class, options = {}) ⇒ Object



47
48
49
50
51
# File 'lib/openstax/rescue_from.rb', line 47

def register_unrecognized_exception(exception_class, options = {})
  unless registered_exceptions.keys.include?(exception_class)
    register_exception(exception_class, options)
  end
end

.registered_exceptionsObject



68
69
70
# File 'lib/openstax/rescue_from.rb', line 68

def registered_exceptions
  @@registered_exceptions.dup
end

.sorry(exception_name) ⇒ Object



94
95
96
# File 'lib/openstax/rescue_from.rb', line 94

def sorry(exception_name)
  @@registered_exceptions[exception_name].sorry
end

.status(exception_name) ⇒ Object



90
91
92
# File 'lib/openstax/rescue_from.rb', line 90

def status(exception_name)
  @@registered_exceptions[exception_name].status_code
end

.this(background = true) ⇒ Object

For rescuing from specific blocks of code: OpenStax::RescueFrom.this …



54
55
56
57
58
59
60
# File 'lib/openstax/rescue_from.rb', line 54

def this(background = true)
  begin
    yield
  rescue Exception => ex
    background ? perform_background_rescue(ex) : perform_rescue(ex)
  end
end

.translate_status_codes(map = {}) ⇒ Object



62
63
64
65
66
# File 'lib/openstax/rescue_from.rb', line 62

def translate_status_codes(map = {})
  map.each do |k, v|
    friendly_status_messages[k] = v
  end
end