Module: Legion::CLI::ErrorForwarder

Defined in:
lib/legion/cli/error_forwarder.rb

Class Method Summary collapse

Class Method Details

.daemon_portObject



54
55
56
57
58
59
60
61
# File 'lib/legion/cli/error_forwarder.rb', line 54

def daemon_port
  require 'legion/settings'
  Legion::Settings.load unless Legion::Settings.instance_variable_get(:@loader)
  api_settings = Legion::Settings[:api]
  (api_settings.is_a?(Hash) && api_settings[:port]) || 4567
rescue StandardError
  4567
end

.forward_error(exception, command: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/legion/cli/error_forwarder.rb', line 10

def forward_error(exception, command: nil)
  payload = {
    level:           'error',
    message:         exception.message.to_s,
    exception_class: exception.class.name,
    backtrace:       Array(exception.backtrace).first(10),
    component_type:  'cli',
    source:          ::File.basename($PROGRAM_NAME)
  }
  payload[:command] = command if command
  post_to_daemon(payload)
rescue StandardError
  # silently swallow — forwarding must never crash the CLI
end

.forward_warning(message, command: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/legion/cli/error_forwarder.rb', line 25

def forward_warning(message, command: nil)
  payload = {
    level:          'warn',
    message:        message.to_s,
    component_type: 'cli',
    source:         ::File.basename($PROGRAM_NAME)
  }
  payload[:command] = command if command
  post_to_daemon(payload)
rescue StandardError
  # silently swallow — forwarding must never crash the CLI
end

.post_to_daemon(payload) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/cli/error_forwarder.rb', line 38

def post_to_daemon(payload)
  port = daemon_port
  uri  = URI("http://localhost:#{port}/api/logs")

  http              = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = 2
  http.read_timeout = 2

  request      = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json')
  request.body = ::JSON.generate(payload)

  http.request(request)
rescue StandardError
  nil
end