Skip to content

Commit

Permalink
Improved error reporting when a parameter cannot be found
Browse files Browse the repository at this point in the history
 * Fixes #8
 * A error inheriting from VagrantError is raised with a nice
   (an translatable) error message.
  • Loading branch information
maoueh committed Oct 18, 2013
1 parent fb4e194 commit f54bac0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
27 changes: 19 additions & 8 deletions lib/nugrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,26 @@ class KeyError < IndexError
end
end

module Nugrant
def self.create_parameters(options)
config = Nugrant::Config.new(options)

return Nugrant::Parameters.new(config)
end

def self.setup_i18n()
I18n.load_path << File.expand_path("locales/en.yml", Nugrant.source_root)
I18n.reload!
end

def self.source_root
@source_root ||= Pathname.new(File.expand_path("../../", __FILE__))
end
end

if defined?(Vagrant)
Nugrant.setup_i18n()

case
when defined?(Vagrant::Plugin::V2)
require 'nugrant/vagrant/v2/plugin'
Expand All @@ -17,11 +36,3 @@ class KeyError < IndexError
abort("You are trying to use Nugrant with an unsupported Vagrant version [#{Vagrant::VERSION}]")
end
end

module Nugrant
def self.create_parameters(options)
config = Nugrant::Config.new(options)

return Nugrant::Parameters.new(config)
end
end
15 changes: 15 additions & 0 deletions lib/nugrant/vagrant/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'vagrant/errors'

module Nugrant
module Vagrant
module Errors
class NugrantVagrantError < ::Vagrant::Errors::VagrantError
error_namespace("nugrant.vagrant.errors")
end

class ParameterNotFoundError < NugrantVagrantError
error_key(:parameter_not_found)
end
end
end
end
5 changes: 5 additions & 0 deletions lib/nugrant/vagrant/v1/config/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'nugrant'
require 'nugrant/vagrant/errors'

module Nugrant
module Vagrant
Expand All @@ -13,10 +14,14 @@ def initialize()

def [](param_name)
return @parameters[param_name]
rescue KeyError
raise Errors::ParameterNotFoundError, :key => param_name
end

def method_missing(method, *args, &block)
return @parameters.method_missing(method, *args, &block)
rescue KeyError
raise Errors::ParameterNotFoundError, :key => method
end

def each(&block)
Expand Down
5 changes: 5 additions & 0 deletions lib/nugrant/vagrant/v2/config/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'nugrant'
require 'nugrant/vagrant/errors'

module Nugrant
module Vagrant
Expand All @@ -13,10 +14,14 @@ def initialize()

def [](param_name)
return @parameters[param_name]
rescue KeyError
raise Errors::ParameterNotFoundError, :key => param_name
end

def method_missing(method, *args, &block)
return @parameters.method_missing(method, *args, &block)
rescue KeyError
raise Errors::ParameterNotFoundError, :key => method
end

def each(&block)
Expand Down
10 changes: 10 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
en:
nugrant:
vagrant:
errors:
parameter_not_found: |-
Nugrant: Parameter '%{key}' was not found, is it defined in
your .vagrantuser file?
If you think it should be found, don't hesitate to fill an
issue @ https://github.com/maoueh/nugrant/issues.

0 comments on commit f54bac0

Please sign in to comment.