From 4a4183398b8cec438ac293d391ee4f958408263b Mon Sep 17 00:00:00 2001 From: Zachary Flower Date: Thu, 14 Dec 2017 14:57:21 -0700 Subject: [PATCH] Add a clean error message for invalid IP addresses --- lib/vagrant/errors.rb | 4 ++++ plugins/providers/virtualbox/action/network.rb | 8 +++++++- templates/locales/en.yml | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index ab535640c..445d58d26 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -448,6 +448,10 @@ module Vagrant error_key(:collides, "vagrant.actions.vm.host_only_network") end + class NetworkAddressInvalid < VagrantError + error_key(:network_address_invalid) + end + class NetworkDHCPAlreadyAttached < VagrantError error_key(:dhcp_already_attached, "vagrant.actions.vm.network") end diff --git a/plugins/providers/virtualbox/action/network.rb b/plugins/providers/virtualbox/action/network.rb index 9618567ca..58c63b726 100644 --- a/plugins/providers/virtualbox/action/network.rb +++ b/plugins/providers/virtualbox/action/network.rb @@ -1,4 +1,5 @@ require "ipaddr" +require "resolv" require "set" require "log4r" @@ -258,7 +259,12 @@ module VagrantPlugins # Default IP is in the 20-bit private network block for DHCP based networks options[:ip] = "172.28.128.1" if options[:type] == :dhcp && !options[:ip] - ip = IPAddr.new(options[:ip]) + begin + ip = IPAddr.new(options[:ip]) + rescue IPAddr::InvalidAddressError => e + raise Vagrant::Errors::NetworkAddressInvalid, :ip => options[:ip], :error_msg => e.message + end + if ip.ipv4? options[:netmask] ||= "255.255.255.0" diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 5608c3814..72e6ee730 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -891,6 +891,11 @@ en: %{message} network_type_not_supported: |- The %{type} network type is not supported for this box or guest. + network_address_invalid: |- + The IP address '%{ip}' is not valid. Please review the error message + below to help resolve the issue: + + %{error_msg} network_manager_not_installed: |- Vagrant was instructed to configure the %{device} network device to be managed by NetworkManager. However, the configured guest VM does