From fd3d78746c6a68db2ebae2f4fa650bf8fa239263 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Wed, 25 Apr 2018 17:27:29 -0700 Subject: [PATCH] (#9717) Improve messaging around not finding requested provider This commit adds a bit more helpful messaging for the user when asking for a provider that might not exist or does not follow the correct casing. If it can find a match on a provider it knows about, it will suggest it. --- lib/vagrant/errors.rb | 4 ++++ lib/vagrant/vagrantfile.rb | 15 ++++++++++++++- templates/locales/en.yml | 9 +++++++++ test/unit/vagrant/vagrantfile_test.rb | 7 +++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index bd9e67c26..3b2e979af 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -580,6 +580,10 @@ module Vagrant error_key(:provider_not_found) end + class ProviderNotFoundSuggestion < VagrantError + error_key(:provider_not_found_suggestion) + end + class ProviderNotUsable < VagrantError error_key(:provider_not_usable) end diff --git a/lib/vagrant/vagrantfile.rb b/lib/vagrant/vagrantfile.rb index eb2890966..bb7367ab7 100644 --- a/lib/vagrant/vagrantfile.rb +++ b/lib/vagrant/vagrantfile.rb @@ -125,8 +125,21 @@ module Vagrant if provider != nil provider_plugin = Vagrant.plugin("2").manager.providers[provider] if !provider_plugin + providers = Vagrant.plugin("2").manager.providers.to_hash.keys + if providers + providers_str = providers.join(', ') + else + providers_str = "N/A" + end + + if providers.include? provider.downcase + raise Errors::ProviderNotFoundSuggestion, + machine: name, provider: provider, + suggestion: provider.downcase, providers: providers_str + end + raise Errors::ProviderNotFound, - machine: name, provider: provider + machine: name, provider: provider, providers: providers_str end provider_cls = provider_plugin[0] diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 330624287..4cc23771e 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1172,6 +1172,15 @@ en: provider_not_found: |- The provider '%{provider}' could not be found, but was requested to back the machine '%{machine}'. Please use a provider that exists. + + Vagrant knows about the following providers: %{providers} + provider_not_found_suggestion: |- + The provider '%{provider}' could not be found, but was requested to + back the machine '%{machine}'. Please use a provider that exists. + + Did you mean '%{suggestion}'? + + Vagrant knows about the following providers: %{providers} provider_not_usable: |- The provider '%{provider}' that was requested to back the machine '%{machine}' is reporting that it isn't usable on this system. The diff --git a/test/unit/vagrant/vagrantfile_test.rb b/test/unit/vagrant/vagrantfile_test.rb index e1e2b688c..f901413f4 100644 --- a/test/unit/vagrant/vagrantfile_test.rb +++ b/test/unit/vagrant/vagrantfile_test.rb @@ -329,6 +329,13 @@ describe Vagrant::Vagrantfile do to raise_error(Vagrant::Errors::ProviderNotFound) end + it "raises an error if the provider is not found but gives suggestion" do + register_provider("foo") + + expect { subject.machine_config(:default, :Foo, boxes) }. + to raise_error(Vagrant::Errors::ProviderNotFoundSuggestion) + end + it "raises an error if the provider is not usable" do register_provider("foo", nil, unusable: true)