Skip to content

Commit

Permalink
move ValidationFailed into chef's exception hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsdeleo committed Mar 30, 2010
1 parent 3fe2bc7 commit 08e0719
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
2 changes: 2 additions & 0 deletions chef/lib/chef/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ class CouchDBNotFound < RuntimeError; end
class PrivateKeyMissing < RuntimeError; end
class CannotWritePrivateKey < RuntimeError; end
class RoleNotFound < RuntimeError; end

class ValidationFailed < ArgumentError; end
end
end
18 changes: 8 additions & 10 deletions chef/lib/chef/mixin/params_validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
# limitations under the License.

class Chef
class ValidationFailed < ArgumentError
end

module Mixin
module ParamsValidate
Expand Down Expand Up @@ -112,7 +110,7 @@ def _pv_required(opts, key, is_required=true)
(opts.has_key?(key.to_sym) && !opts[key.to_sym].nil?)
true
else
raise ValidationFailed, "Required argument #{key} is missing!"
raise Exceptions::ValidationFailed, "Required argument #{key} is missing!"
end
end
end
Expand All @@ -125,7 +123,7 @@ def _pv_equal_to(opts, key, to_be)
passes = true if value == tb
end
unless passes
raise ValidationFailed, "Option #{key} must be equal to one of: #{to_be.join(", ")}! You passed #{value.inspect}."
raise Exceptions::ValidationFailed, "Option #{key} must be equal to one of: #{to_be.join(", ")}! You passed #{value.inspect}."
end
end
end
Expand All @@ -139,7 +137,7 @@ def _pv_kind_of(opts, key, to_be)
passes = true if value.kind_of?(tb)
end
unless passes
raise ValidationFailed, "Option #{key} must be a kind of #{to_be}! You passed #{value.inspect}."
raise Exceptions::ValidationFailed, "Option #{key} must be a kind of #{to_be}! You passed #{value.inspect}."
end
end
end
Expand All @@ -150,14 +148,14 @@ def _pv_respond_to(opts, key, method_name_list)
unless value.nil?
Array(method_name_list).each do |method_name|
unless value.respond_to?(method_name)
raise ValidationFailed, "Option #{key} must have a #{method_name} method!"
raise Exceptions::ValidationFailed, "Option #{key} must have a #{method_name} method!"
end
end
end
end

# Assert that parameter returns false when passed a predicate method.
# For example, :cannot_be => :blank will raise a ValidationFailed
# For example, :cannot_be => :blank will raise a Exceptions::ValidationFailed
# error value.blank? returns a 'truthy' (not nil or false) value.
#
# Note, this will *PASS* if the object doesn't respond to the method.
Expand All @@ -169,7 +167,7 @@ def _pv_cannot_be(opts, key, predicate_method_base_name)

if value.respond_to?(predicate_method)
if value.send(predicate_method)
raise ValidationFailed, "Option #{key} cannot be #{predicate_method_base_name}"
raise Exceptions::ValidationFailed, "Option #{key} cannot be #{predicate_method_base_name}"
end
end
end
Expand All @@ -195,7 +193,7 @@ def _pv_regex(opts, key, regex)
end
end
unless passes
raise ValidationFailed, "Option #{key}'s value #{value} does not match regular expression #{regex.to_s}"
raise Exceptions::ValidationFailed, "Option #{key}'s value #{value} does not match regular expression #{regex.to_s}"
end
end
end
Expand All @@ -207,7 +205,7 @@ def _pv_callbacks(opts, key, callbacks)
if value != nil
callbacks.each do |message, zeproc|
if zeproc.call(value) != true
raise ValidationFailed, "Option #{key}'s value #{value} #{message}!"
raise Exceptions::ValidationFailed, "Option #{key}'s value #{value} #{message}!"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion chef/spec/unit/mixin/params_validate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def music(is_good=true)
end.should_not raise_error
lambda do
@vo.validate({:not_blank => ""}, {:not_blank => {:cannot_be => :blank}})
end.should raise_error(Chef::ValidationFailed)
end.should raise_error(Chef::Exceptions::ValidationFailed)
end

it "should set and return a value, then return the same value" do
Expand Down
2 changes: 1 addition & 1 deletion chef/spec/unit/node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
end

it "cannot be blank" do
lambda { @node.name("")}.should raise_error(Chef::ValidationFailed)
lambda { @node.name("")}.should raise_error(Chef::Exceptions::ValidationFailed)
end
end

Expand Down

0 comments on commit 08e0719

Please sign in to comment.