module OpenStax::RescueFrom

Constants

VERSION

Public Class Methods

configuration() click to toggle source
# File lib/openstax/rescue_from.rb, line 106
def configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source
# File lib/openstax/rescue_from.rb, line 102
def configure
  yield configuration
end
do_not_reraise() { || ... } click to toggle source

Not threadsafe

# File lib/openstax/rescue_from.rb, line 25
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) click to toggle source
# File lib/openstax/rescue_from.rb, line 94
def extras_proc(exception_name)
  options_for(exception_name).extras
end
friendly_message(proxy) click to toggle source
# File lib/openstax/rescue_from.rb, line 72
def friendly_message(proxy)
  options_for(proxy.name).message ||
    friendly_status_messages[proxy.status] ||
      default_friendly_message
end
generate_id() click to toggle source
# File lib/openstax/rescue_from.rb, line 98
def generate_id
  sprintf "%06d", "#{SecureRandom.random_number(10**6)}"
end
http_code(status) click to toggle source
# File lib/openstax/rescue_from.rb, line 90
def http_code(status)
  Rack::Utils.status_code(status)
end
non_notifying_exceptions() click to toggle source
# File lib/openstax/rescue_from.rb, line 64
def non_notifying_exceptions
  @@registered_exceptions.reject { |_, v| v.notify? }.keys
end
notifies_for?(exception_name) click to toggle source
# File lib/openstax/rescue_from.rb, line 78
def notifies_for?(exception_name)
  options_for(exception_name).notify?
end
notifying_exceptions() click to toggle source
# File lib/openstax/rescue_from.rb, line 68
def notifying_exceptions
  @@registered_exceptions.select { |_, v| v.notify? }.keys
end
perform_background_rescue(exception, listener = MuteListener.new) click to toggle source
# File lib/openstax/rescue_from.rb, line 17
def perform_background_rescue(exception, listener = MuteListener.new)
  proxy = ExceptionProxy.new(exception)
  log_background_system_error(proxy)
  send_notifying_background_exceptions(proxy)
  finish_background_exception_rescue(proxy, listener)
end
perform_rescue(exception, listener = MuteListener.new) click to toggle source
# File lib/openstax/rescue_from.rb, line 10
def perform_rescue(exception, listener = MuteListener.new)
  proxy = ExceptionProxy.new(exception)
  log_system_error(proxy)
  send_notifying_exceptions(proxy, listener)
  finish_exception_rescue(proxy, listener)
end
register_exception(exception, options = {}) click to toggle source
# File lib/openstax/rescue_from.rb, line 38
def register_exception(exception, options = {})
  name = exception.is_a?(String) ? exception : exception.name
  options = ExceptionOptions.new(options)
  @@registered_exceptions ||= {}
  @@registered_exceptions[name] = options
end
registered_exceptions() click to toggle source
# File lib/openstax/rescue_from.rb, line 60
def registered_exceptions
  @@registered_exceptions.dup
end
sorry(exception_name) click to toggle source
# File lib/openstax/rescue_from.rb, line 86
def sorry(exception_name)
  options_for(exception_name).sorry
end
status(exception_name) click to toggle source
# File lib/openstax/rescue_from.rb, line 82
def status(exception_name)
  options_for(exception_name).status_code
end
this(background = true) { || ... } click to toggle source

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

# File lib/openstax/rescue_from.rb, line 46
def this(background = true)
  begin
    yield
  rescue Exception => ex
    background ? perform_background_rescue(ex) : perform_rescue(ex)
  end
end
translate_status_codes(map = {}) click to toggle source
# File lib/openstax/rescue_from.rb, line 54
def translate_status_codes(map = {})
  map.each do |k, v|
    friendly_status_messages[k] = v
  end
end

Private Class Methods

default_friendly_message() click to toggle source
# File lib/openstax/rescue_from.rb, line 126
def default_friendly_message
  "Sorry, #{configuration.app_name} had some unexpected trouble with your request."
end
finish_background_exception_rescue(proxy, listener) click to toggle source
# File lib/openstax/rescue_from.rb, line 168
def finish_background_exception_rescue(proxy, listener)
  if configuration.raise_background_exceptions
    raise proxy.exception
  else
    listener.openstax_exception_rescued(proxy, notifies_for?(proxy.name))
  end
end
finish_exception_rescue(proxy, listener) click to toggle source
# File lib/openstax/rescue_from.rb, line 160
def finish_exception_rescue(proxy, listener)
  if configuration.raise_exceptions
    raise proxy.exception
  else
    listener.openstax_exception_rescued(proxy, notifies_for?(proxy.name))
  end
end
friendly_status_messages() click to toggle source
# File lib/openstax/rescue_from.rb, line 116
def friendly_status_messages
  @@friendly_status_messages ||= {
    internal_server_error: default_friendly_message,
    not_found: 'We could not find the requested information.',
    bad_request: 'The request was unrecognized.',
    forbidden: 'You are not allowed to do that.',
    unprocessable_entity: 'Your browser asked for something that we cannot do.'
  }
end
log_background_system_error(proxy) click to toggle source
# File lib/openstax/rescue_from.rb, line 141
def log_background_system_error(proxy)
  return unless notifies_for?(proxy.name)

  logger = Logger.new(proxy)
  logger.record_system_error!('A background job exception occurred')
end
log_system_error(proxy) click to toggle source
# File lib/openstax/rescue_from.rb, line 134
def log_system_error(proxy)
  return unless notifies_for?(proxy.name)

  logger = Logger.new(proxy)
  logger.record_system_error!
end
options_for(name) click to toggle source
# File lib/openstax/rescue_from.rb, line 112
def options_for(name)
  @@registered_exceptions[name] || ExceptionOptions.new
end
resolve_ip(ip) click to toggle source
# File lib/openstax/rescue_from.rb, line 130
def resolve_ip(ip)
  Resolv.getname(ip) rescue 'unknown'
end
send_notifying_background_exceptions(proxy) click to toggle source
# File lib/openstax/rescue_from.rb, line 154
def send_notifying_background_exceptions(proxy)
  return if !configuration.notify_background_exceptions || !notifies_for?(proxy.name)

  instance_exec(proxy, &configuration.notify_background_proc)
end
send_notifying_exceptions(proxy, controller) click to toggle source
# File lib/openstax/rescue_from.rb, line 148
def send_notifying_exceptions(proxy, controller)
  return if !configuration.notify_exceptions || !notifies_for?(proxy.name)

  instance_exec(proxy, controller, &configuration.notify_proc)
end