Class: TestServer::ErrorHandler
- Inherits:
-
Object
- Object
- TestServer::ErrorHandler
- Defined in:
- lib/test_server/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
-
#backtrace ⇒ Object
Returns the value of attribute backtrace.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#exit_code ⇒ Object
readonly
Returns the value of attribute exit_code.
-
#original_message ⇒ Object
Returns the value of attribute original_message.
-
#status_code ⇒ Object
readonly
Returns the value of attribute status_code.
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_hash ⇒ Object
- #to_json ⇒ Object
- #use(data) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ErrorHandler
21 22 23 24 25 26 27 28 29 |
# File 'lib/test_server/error_handler.rb', line 21 def initialize( = {}) @exception = .fetch(:exception) @details_i18n = .fetch(:details) @summary_i18n = .fetch(:summary) @exit_code = .fetch(:exit_code, 1) @status_code = .fetch(:status_code, :internal_server_error) rescue KeyError => e raise ArgumentError, e. end |
Class Attribute Details
.handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
9 10 11 |
# File 'lib/test_server/error_handler.rb', line 9 def handlers @handlers end |
.mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
9 10 11 |
# File 'lib/test_server/error_handler.rb', line 9 def mutex @mutex end |
Instance Attribute Details
#backtrace ⇒ Object
Returns the value of attribute backtrace.
19 20 21 |
# File 'lib/test_server/error_handler.rb', line 19 def backtrace @backtrace end |
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
18 19 20 |
# File 'lib/test_server/error_handler.rb', line 18 def exception @exception end |
#exit_code ⇒ Object (readonly)
Returns the value of attribute exit_code.
18 19 20 |
# File 'lib/test_server/error_handler.rb', line 18 def exit_code @exit_code end |
#original_message ⇒ Object
Returns the value of attribute original_message.
19 20 21 |
# File 'lib/test_server/error_handler.rb', line 19 def end |
#status_code ⇒ Object (readonly)
Returns the value of attribute status_code.
18 19 20 |
# File 'lib/test_server/error_handler.rb', line 18 def status_code @status_code end |
Class Method Details
.create(options = {}, &block) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/test_server/error_handler.rb', line 32 def create( = {}, &block) handler = new(, &block) handlers << handler handler end |
.find(exception) ⇒ Object
39 40 41 |
# File 'lib/test_server/error_handler.rb', line 39 def find(exception) handlers.find(proc { default_handler }) { |h| h.exception == exception } end |
Instance Method Details
#details(format = :plain) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/test_server/error_handler.rb', line 57 def details(format = :plain) case format when :plain @details when :html Rack::Utils.escape_html(@details) else @details end end |
#execute(data = {}) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/test_server/error_handler.rb', line 87 def execute(data = {}) use(data) TestServer.ui_logger.fatal details TestServer.ui_logger.debug if TestServer.ui_logger.debug "Original Backtrace follows\n" + backtrace.join("\n") if backtrace Kernel.exit exit_code end |
#summary(format = :plain) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/test_server/error_handler.rb', line 68 def summary(format = :plain) case format when :plain @summary when :html Rack::Utils.escape_html(@summary) else @summary end end |
#to_hash ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/test_server/error_handler.rb', line 96 def to_hash ErrorHandler.mutex.synchronize do @details ||= I18n.t(details_i18n) @summary ||= I18n.t(summary_i18n) end { error_summary: summary, error_details: details, result: :failure, } end |
#to_json ⇒ Object
109 110 111 |
# File 'lib/test_server/error_handler.rb', line 109 def to_json JSON.dump(to_hash) end |
#use(data) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/test_server/error_handler.rb', line 79 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 |