Class: LocalPac::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/local_pac/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/local_pac/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/local_pac/error_handler.rb', line 9

def handlers
  @handlers
end

.mutexObject (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

#exceptionObject (readonly)

Returns the value of attribute exception.



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

def exception
  @exception
end

#original_messageObject

Returns the value of attribute original_message.



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

def original_message
  @original_message
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(options = {}, &block)
  handler = new(options, &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 original_message if original_message
  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_jsonObject



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