Module: TrainPlugins::Juniper::ErrorHandling

Included in:
Connection
Defined in:
lib/train-juniper/connection/error_handling.rb

Overview

Handles error detection, classification, and messaging

Constant Summary collapse

JUNOS_ERRORS =

JunOS error patterns organized by type

{
  configuration: [/^error:/i, /configuration database locked/i],
  syntax: [/syntax error/i],
  command: [/invalid command/i, /unknown command/i],
  argument: [/missing argument/i]
}.freeze
JUNOS_ERROR_PATTERNS =

Flattened error patterns for quick matching

JUNOS_ERRORS.values.flatten.freeze

Instance Method Summary collapse

Instance Method Details

#bastion_auth_error?(error) ⇒ Boolean

Check if error is bastion authentication related



43
44
45
46
# File 'lib/train-juniper/connection/error_handling.rb', line 43

def bastion_auth_error?(error)
  @options[:bastion_host] &&
    (error.message.include?('Permission denied') || error.message.include?('command failed'))
end

#bastion_error_message(error) ⇒ String

Build helpful bastion error message



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/train-juniper/connection/error_handling.rb', line 51

def bastion_error_message(error)
  "    Failed to connect to Juniper device \#{@options[:host]} via bastion \#{@options[:bastion_host]}: \#{error.message}\n\n    Possible causes:\n    1. Incorrect bastion credentials (user: \#{@options[:bastion_user] || @options[:user]})\n    2. Network connectivity issues to bastion host\n    3. Bastion host SSH service not available on port \#{@options[:bastion_port]}\n    4. Target device not reachable from bastion\n\n    Authentication options:\n    - Password: Use --bastion-password (or JUNIPER_BASTION_PASSWORD env var)\n    - SSH Key: Use --key-files option to specify SSH private key files\n    - SSH Agent: Ensure your SSH agent has the required keys loaded\n\n    For more details, see: https://mitre.github.io/train-juniper/troubleshooting/#bastion-authentication\n  ERROR\nend\n"

#handle_connection_error(error) ⇒ Object

Handle connection errors with helpful messages

Raises:

  • (Train::TransportError)

    Always raises with formatted message



30
31
32
33
34
35
36
37
38
# File 'lib/train-juniper/connection/error_handling.rb', line 30

def handle_connection_error(error)
  @logger.error("SSH connection failed: #{error.message}")

  if bastion_auth_error?(error)
    raise Train::TransportError, bastion_error_message(error)
  else
    raise Train::TransportError, "Failed to connect to Juniper device #{@options[:host]}: #{error.message}"
  end
end

#junos_error?(output) ⇒ Boolean

Check for JunOS error patterns



21
22
23
24
25
# File 'lib/train-juniper/connection/error_handling.rb', line 21

def junos_error?(output)
  return false if output.nil? || output.empty?

  JUNOS_ERROR_PATTERNS.any? { |pattern| output.match?(pattern) }
end