Class: LocalPac::ErrorHandler
- Inherits:
-
Object
- Object
- LocalPac::ErrorHandler
- Defined in:
- lib/local_pac/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
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#original_message ⇒ Object
Returns the value of attribute original_message.
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/local_pac/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/local_pac/error_handler.rb', line 9 def handlers @handlers end |
.mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
9 10 11 |
# File 'lib/local_pac/error_handler.rb', line 9 def mutex @mutex end |
Instance Attribute Details
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
18 19 20 |
# File 'lib/local_pac/error_handler.rb', line 18 def exception @exception end |
#original_message ⇒ Object
Returns the value of attribute original_message.
19 20 21 |
# File 'lib/local_pac/error_handler.rb', line 19 def end |
Class Method Details
.create(options = {}, &block) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/local_pac/error_handler.rb', line 31 def create( = {}, &block) handler = new(, &block) handlers << handler handler end |
.find(exception) ⇒ Object
38 39 40 |
# File 'lib/local_pac/error_handler.rb', line 38 def find(exception) handlers.find(proc { default_handler }) { |h| h.exception == exception } end |
Instance Method Details
#details(format = :plain) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/local_pac/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
85 86 87 88 89 90 91 |
# File 'lib/local_pac/error_handler.rb', line 85 def execute(data = {}) use(data) LocalPac.ui_logger.fatal details LocalPac.ui_logger.debug if Kernel.exit exit_code end |
#summary(format = :plain) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/local_pac/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
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/local_pac/error_handler.rb', line 93 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 |
# File 'lib/local_pac/error_handler.rb', line 78 def use(data) data = JSON.parse(data).symbolize_keys if data.kind_of? String @details = I18n.t(details_i18n, data) @summary = I18n.t(summary_i18n, data) end |