diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2020-02-03 19:42:28 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2020-02-03 19:42:28 -0800 |
commit | 9d4de20fafd889f4853b2d044ddb815bb3a6864a (patch) | |
tree | da25be2a7c0fa0794e8c3073d2236fa1fe4fce1d | |
parent | 7149e1aa6c8f1617bd561c52703fbcb67c7d66c8 (diff) | |
download | chef-lcg/better-raises.tar.gz |
Add information about the resource_name in property exceptionslcg/better-raises
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/property.rb | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/chef/property.rb b/lib/chef/property.rb index 34fde05020..93e75e8810 100644 --- a/lib/chef/property.rb +++ b/lib/chef/property.rb @@ -128,7 +128,7 @@ class Chef if options.key?(:name_attribute) # If we have both name_attribute and name_property and they differ, raise an error if options.key?(:name_property) - raise ArgumentError, "name_attribute and name_property are functionally identical and both cannot be specified on a property at once. Use just one on property #{self}" + raise ArgumentError, "name_attribute and name_property are functionally identical and both cannot be specified on a property at once. Use just one on property #{self} of resource #{resource_name}" end # replace name_property with name_attribute in place @@ -137,7 +137,7 @@ class Chef end if options.key?(:default) && options.key?(:name_property) - raise ArgumentError, "A property cannot be both a name_property/name_attribute and have a default value. Use one or the other on property #{self}" + raise ArgumentError, "A property cannot be both a name_property/name_attribute and have a default value. Use one or the other on property #{self} of resource #{resource_name}" end # Recursively freeze the default if it isn't a lazy value. @@ -406,7 +406,8 @@ class Chef end if value.nil? && required? - raise Chef::Exceptions::ValidationFailed, "#{name} is a required property" + resource_str = resource_name ? " of the #{resource_name} resource" : "" + raise Chef::Exceptions::ValidationFailed, "#{name} is a required property#{resource_str}" else value end @@ -435,7 +436,8 @@ class Chef end if value.nil? && required? - raise Chef::Exceptions::ValidationFailed, "#{name} is a required property" + resource_str = resource_name ? " of the #{resource_name} resource" : "" + raise Chef::Exceptions::ValidationFailed, "#{name} is a required property#{resource_str}" else value end @@ -546,6 +548,15 @@ class Chef self.class.new(**options.merge(modified_options)) end + # Helper class for safely requesting a resource_name. + # + # @api private + def resource_name + if declared_in + declared_in.respond_to?(:resource_name) ? declared_in.resource_name : declared_in + end + end + # # Emit the DSL for this property into the resource class (`declared_in`). # @@ -562,7 +573,6 @@ class Chef # Object#hash method on the resource, and overriding that with a property will cause # very confusing results. if property_redefines_method? - resource_name = declared_in.respond_to?(:resource_name) ? declared_in.resource_name : declared_in raise ArgumentError, "Property `#{name}` of resource `#{resource_name}` overwrites an existing method. A different name should be used for this property." end |