Class: ProxyTester::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_tester/error_handler.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



21
22
23
24
25
26
27
28
# File 'lib/proxy_tester/error_handler.rb', line 21

def initialize(options = {})
  @exception    = options.fetch(:exception)
  @details_i18n = options.fetch(:details)
  @summary_i18n = options.fetch(:summary)
  @exit_code    = options.fetch(:exit_code)
rescue KeyError => e
  raise ArgumentError, e.message
end

Class Attribute Details

.handlersObject (readonly)

Returns the value of attribute handlers.



9
10
11
# File 'lib/proxy_tester/error_handler.rb', line 9

def handlers
  @handlers
end

.mutexObject (readonly)

Returns the value of attribute mutex.



9
10
11
# File 'lib/proxy_tester/error_handler.rb', line 9

def mutex
  @mutex
end

Instance Attribute Details

#causeObject

Returns the value of attribute cause.



19
20
21
# File 'lib/proxy_tester/error_handler.rb', line 19

def cause
  @cause
end

#exceptionObject (readonly)

Returns the value of attribute exception.



18
19
20
# File 'lib/proxy_tester/error_handler.rb', line 18

def exception
  @exception
end

Class Method Details

.create(options = {}, &block) ⇒ Object



31
32
33
34
35
36
# File 'lib/proxy_tester/error_handler.rb', line 31

def create(options = {}, &block)
  handler = new(options, &block)
  handlers << handler

  handler
end

.find(exception) ⇒ Object



38
39
40
# File 'lib/proxy_tester/error_handler.rb', line 38

def find(exception)
  handlers.find(proc { default_handler }) { |h| h.exception == exception.class }
end

Instance Method Details

#details(format = :plain) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/proxy_tester/error_handler.rb', line 56

def details(format = :plain)
  case format
  when :plain
    @details
  when :html
    Rack::Utils.escape_html(@details)
  else
    @details
  end
end

#execute(data = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/proxy_tester/error_handler.rb', line 86

def execute(data = {})
  use(data)

  ProxyTester.ui_logger.fatal details
  ProxyTester.ui_logger.debug "Resulting Exception: #{exception}"
  ProxyTester.ui_logger.debug "Resulting Exit Code: #{exit_code}"

  if cause
    ProxyTester.ui_logger.debug "Cause: #{cause.class}"
    ProxyTester.ui_logger.debug "Cause Message: #{cause.message}"
    ProxyTester.ui_logger.debug "Cause Backtrace:\n" + Array(cause.backtrace).join("\n")
  end

  Kernel.exit exit_code
end

#summary(format = :plain) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/proxy_tester/error_handler.rb', line 67

def summary(format = :plain)
  case format
  when :plain
    @summary
  when :html
    Rack::Utils.escape_html(@summary)
  else
    @summary
  end
end

#to_jsonObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/proxy_tester/error_handler.rb', line 102

def to_json
  ErrorHandler.mutex.synchronize do
    @details ||= I18n.t(details_i18n)
    @summary ||= I18n.t(summary_i18n)
  end

  JSON.dump(
      error_summary: summary,
      error_details: details,
      result: :failure,
  )
end

#use(data) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/proxy_tester/error_handler.rb', line 78

def use(data)
  data = JSON.parse(data) if data.kind_of? String
  data = data.symbolize_keys 

  @details ||= I18n.t(details_i18n, data)
  @summary ||= I18n.t(summary_i18n, data)
end