Module: Forger::Create::ErrorMessages

Included in:
Forger::Create
Defined in:
lib/forger/create/error_messages.rb

Instance Method Summary collapse

Instance Method Details

#handle_ec2_service_error!(exception) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/forger/create/error_messages.rb', line 5

def handle_ec2_service_error!(exception)
  meth = map_exception_to_method(exception)
  if respond_to?(meth)
    message = send(meth) # custom specific error message
    message = print_error_message(exception, message)
  else
    # generic error message
    print_error_message(exception, <<-EOL)
There was an error with the parameters used for the run_instance method.
EOL
  end
end

#invalid_group_not_foundObject

specific messages with a little more info for more common error cases below:



37
38
39
40
41
42
# File 'lib/forger/create/error_messages.rb', line 37

def invalid_group_not_found
  <<-EOL
The security group passed in does not exit.
Please double check that security group exists in the VPC.
EOL
end

#invalid_parameter_combinationObject



44
45
46
47
48
49
# File 'lib/forger/create/error_messages.rb', line 44

def invalid_parameter_combination
  <<-EOL
The parameters passed to the run_instances method were invalid.
Please double check that the parameters are all valid.
EOL
end

#invalid_subnet_id_not_foundObject



51
52
53
54
55
56
# File 'lib/forger/create/error_messages.rb', line 51

def invalid_subnet_id_not_found
  <<-EOL
The provided subnets ids were were not found.
Please double check that the subnets exists.
EOL
end

#map_exception_to_method(exception) ⇒ Object

Examples:

Aws::EC2::Errors::InvalidGroupNotFound => invalid_group_not_found
Aws::EC2::Errors::InvalidParameterCombination => invalid_parameter_combination


21
22
23
24
# File 'lib/forger/create/error_messages.rb', line 21

def map_exception_to_method(exception)
  class_name = File.basename(exception.class.to_s).sub(/.*::/,'')
  class_name.underscore # method_name
end


26
27
28
29
30
31
32
33
# File 'lib/forger/create/error_messages.rb', line 26

def print_error_message(exception, message)
  puts "ERROR: Unable to launch the instance.".color(:red)
  puts message
  puts exception.message
  puts "For the full internal backtrace re-run the command with DEBUG=1"
  puts exception.backtrace if ENV['DEBUG']
  exit 1
end