Module: Contingency::Plan

Defined in:
lib/contingency/plan.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/contingency/plan.rb', line 5

def self.included(base)
  base.send(:include, Contingency.adapter)
  base.extend ActiveSupport::Rescuable

  if base.catch_errors?
    Contingency.configuration.errors.each do |code, exceptions|
      base.rescue_from *exceptions, with: ->(exception){ render_error code, exception }
    end
  end
end

Instance Method Details

#errorObject



16
17
18
# File 'lib/contingency/plan.rb', line 16

def error
  render_error(params[:code], Contingency::Exceptions::RenderedErrorPageException.new)
end

#error_report(exception) ⇒ Object



20
21
22
23
24
25
# File 'lib/contingency/plan.rb', line 20

def error_report(exception)
  "#{exception.class} raised on #{request.fullpath}:" \
  "\n#{exception.message}" \
  "\nBacktrace:" \
  "\n#{exception.backtrace.join("\n")}"
end

#render_error(code, exception) ⇒ Object



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

def render_error(code, exception)
  message, description = Contingency.configuration.error_messages.fetch(code, Contingency.configuration.unknown_error_message)
  error_logger = logger.method(code.to_i < 500 ? :info : :error)
  error_logger.call "#{code}: #{description}.\n" + error_report(exception)
  @code = code
  @message = message
  @description = description
  error_renderer(code)
rescue Exception => handler_exception
  logger.fatal error_report(Contingency::Exceptions::ContingencyPlanException.new(exception, handler_exception))
  failure_renderer(500)
end