From cf91f578fb0b67245b7dd2c0615c1a65dfb09a10 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 1 Sep 2010 07:22:14 -0700 Subject: [PATCH] Downloaders now raise exceptions instead of erroring environment --- lib/vagrant/downloaders/file.rb | 4 +--- lib/vagrant/downloaders/http.rb | 2 +- lib/vagrant/errors.rb | 10 ++++++++++ templates/locales/en.yml | 9 +++++++++ templates/strings.yml | 10 ---------- test/vagrant/downloaders/file_test.rb | 7 ++++--- test/vagrant/downloaders/http_test.rb | 6 +++--- 7 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/vagrant/downloaders/file.rb b/lib/vagrant/downloaders/file.rb index 67bfccdca..d14b52368 100644 --- a/lib/vagrant/downloaders/file.rb +++ b/lib/vagrant/downloaders/file.rb @@ -10,9 +10,7 @@ module Vagrant end def prepare(source_url) - if !::File.file?(source_url) - return env.error!(:downloader_file_doesnt_exist, :source_url => source_url) - end + raise Errors::DownloaderFileDoesntExist.new if !::File.file?(source_url) end def download!(source_url, destination_file) diff --git a/lib/vagrant/downloaders/http.rb b/lib/vagrant/downloaders/http.rb index d2b3baef1..212b8d881 100644 --- a/lib/vagrant/downloaders/http.rb +++ b/lib/vagrant/downloaders/http.rb @@ -46,7 +46,7 @@ module Vagrant end end rescue SocketError - env.error!(:box_download_http_socket_error, :box_url => source_url) + raise Errors::DownloaderHTTPSocketError.new end end end diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index ffe550812..265db8bfd 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -79,6 +79,16 @@ module Vagrant error_key(:cli_missing_env) end + class DownloaderFileDoesntExist < VagrantError + status_code(37) + error_key(:file_missing, "vagrant.downloaders.file") + end + + class DownloaderHTTPSocketError < VagrantError + status_code(38) + error_key(:socket_error, "vagrant.downloaders.http") + end + class ForwardPortAutolistEmpty < VagrantError status_code(27) error_key(:auto_empty, "vagrant.actions.vm.forward_ports") diff --git a/templates/locales/en.yml b/templates/locales/en.yml index a001c6c54..ecee2a8ea 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -278,6 +278,15 @@ en: include_file_missing: |- Package include file doesn't exist: %{file} + downloaders: + file: + file_missing: "The specified path to a file doesn't exist." + http: + socket_error: |- + An error occurred while trying to download the specified box. This most + often happens if there is no internet connection or the address is + invalid. + hosts: bsd: nfs_export: diff --git a/templates/strings.yml b/templates/strings.yml index bcdc737bf..970ba1acc 100644 --- a/templates/strings.yml +++ b/templates/strings.yml @@ -7,12 +7,6 @@ #--------------------------------------------------------------------- # CATEGORY: Error Messages #--------------------------------------------------------------------- -:box_download_http_socket_error: |- - An error occurred while trying to download the specified box. This most - often happens if there is no internet connection or the address is - invalid. - - Box URL: <%= box_url %> :box_file_exists: |- The specified output file for packaging already exists. Please move the file or modify the output filename parameter then try to package @@ -52,10 +46,6 @@ vagrant box add name uri vagrant box remove name vagrant box list -:downloader_file_doesnt_exist: |- - The given box does not exist on the file system: - - <%= source_url %> :package_requires_export: |- Package must be used in conjunction with export. :ssh_bad_exit_status: |- diff --git a/test/vagrant/downloaders/file_test.rb b/test/vagrant/downloaders/file_test.rb index eb979034e..5d3ac9355 100644 --- a/test/vagrant/downloaders/file_test.rb +++ b/test/vagrant/downloaders/file_test.rb @@ -9,9 +9,10 @@ class FileDownloaderTest < Test::Unit::TestCase context "preparing" do should "raise an exception if the file does not exist" do File.expects(:file?).with(@uri).returns(false) - @downloader.prepare(@uri) - assert @downloader.env.error? - assert_equal :downloader_file_doesnt_exist, @downloader.env.error.first + + assert_raises(Vagrant::Errors::DownloaderFileDoesntExist) { + @downloader.prepare(@uri) + } end end diff --git a/test/vagrant/downloaders/http_test.rb b/test/vagrant/downloaders/http_test.rb index b854fd6b6..e2fab87f8 100644 --- a/test/vagrant/downloaders/http_test.rb +++ b/test/vagrant/downloaders/http_test.rb @@ -45,10 +45,10 @@ class HttpDownloaderTest < Test::Unit::TestCase should "error environment if invalid URL given" do Net::HTTP.expects(:new).raises(SocketError.new) - @downloader.download!(@uri, @tempfile) - assert @downloader.env.error? - assert_equal :box_download_http_socket_error, @downloader.env.error.first + assert_raises(Vagrant::Errors::DownloaderHTTPSocketError) { + @downloader.download!(@uri, @tempfile) + } end end