Class: ProxyTester::ErrorHandler
- Inherits:
-
Object
- Object
- ProxyTester::ErrorHandler
- Defined in:
- lib/proxy_tester/error_handler.rb
Class Attribute Summary collapse
-
.handlers ⇒ Object
readonly
Returns the value of attribute handlers.
-
.mutex ⇒ Object
readonly
Returns the value of attribute mutex.
Instance Attribute Summary collapse
-
#cause ⇒ Object
Returns the value of attribute cause.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
Class Method Summary collapse
Instance Method Summary collapse
- #details(format = :plain) ⇒ Object
- #execute(data = {}) ⇒ Object
-
#initialize(options = {}) ⇒ ErrorHandler
constructor
A new instance of ErrorHandler.
- #summary(format = :plain) ⇒ Object
- #to_json ⇒ Object
- #use(data) ⇒ Object
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( = {}) @exception = .fetch(:exception) @details_i18n = .fetch(:details) @summary_i18n = .fetch(:summary) @exit_code = .fetch(:exit_code) rescue KeyError => e raise ArgumentError, e. end |
Class Attribute Details
.handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
9 10 11 |
# File 'lib/proxy_tester/error_handler.rb', line 9 def handlers @handlers end |
.mutex ⇒ Object (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
#cause ⇒ Object
Returns the value of attribute cause.
19 20 21 |
# File 'lib/proxy_tester/error_handler.rb', line 19 def cause @cause end |
#exception ⇒ Object (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( = {}, &block) handler = new(, &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_json ⇒ Object
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 |