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

Parameters:

  • error (StandardError)

    The error to check

Returns:

  • (Boolean)

    true if error is bastion-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

Parameters:

  • error (StandardError)

    The original error

Returns:

  • (String)

    Detailed error message with troubleshooting steps



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)
  <<~ERROR
    Failed to connect to Juniper device #{@options[:host]} via bastion #{@options[:bastion_host]}: #{error.message}

    Possible causes:
    1. Incorrect bastion credentials (user: #{@options[:bastion_user] || @options[:user]})
    2. Network connectivity issues to bastion host
    3. Bastion host SSH service not available on port #{@options[:bastion_port]}
    4. Target device not reachable from bastion

    Authentication options:
    - Password: Use --bastion-password (or JUNIPER_BASTION_PASSWORD env var)
    - SSH Key: Use --key-files option to specify SSH private key files
    - SSH Agent: Ensure your SSH agent has the required keys loaded

    For more details, see: https://mitre.github.io/train-juniper/troubleshooting/#bastion-authentication
  ERROR
end

#handle_connection_error(error) ⇒ Object

Handle connection errors with helpful messages

Parameters:

  • error (StandardError)

    The error that occurred

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

Parameters:

  • output (String)

    Command output to check

Returns:

  • (Boolean)

    true if output contains 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