diff options
| author | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-28 15:41:13 -0700 |
|---|---|---|
| committer | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-28 15:41:13 -0700 |
| commit | cd474b0aff0e0fec33295c8abc0668af90fb7cc1 (patch) | |
| tree | 19cf16beee0cd0be4a4535dc6e62eb19817773be | |
| parent | e17f14925db09903b43151843102f204f9b31b61 (diff) | |
| download | puppet-cd474b0aff0e0fec33295c8abc0668af90fb7cc1.tar.gz puppet-cd474b0aff0e0fec33295c8abc0668af90fb7cc1.tar.xz puppet-cd474b0aff0e0fec33295c8abc0668af90fb7cc1.zip | |
maint: better error reporting for argument count mismatch.
Another face quirk in error reporting highlights a better way to work the
error message; do so.
| -rw-r--r-- | lib/puppet/application/face_base.rb | 86 |
1 files changed, 39 insertions, 47 deletions
diff --git a/lib/puppet/application/face_base.rb b/lib/puppet/application/face_base.rb index 69c3ad5ad..b513ba5e3 100644 --- a/lib/puppet/application/face_base.rb +++ b/lib/puppet/application/face_base.rb @@ -213,54 +213,46 @@ class Puppet::Application::FaceBase < Puppet::Application # Call the method associated with the provided action (e.g., 'find'). if @action - begin - # We need to do arity checking here because this is generic code - # calling generic methods – that have argument defaulting. We need to - # make sure we don't accidentally pass the options as the first - # argument to a method that takes one argument. eg: - # - # puppet facts find - # => options => {} - # @arguments => [{}] - # => @face.send :bar, {} - # - # def face.bar(argument, options = {}) - # => bar({}, {}) # oops! we thought the options were the - # # positional argument!! - # - # We could also fix this by making it mandatory to pass the options on - # every call, but that would make the Ruby API much more annoying to - # work with; having the defaulting is a much nicer convention to have. - # - # We could also pass the arguments implicitly, by having a magic - # 'options' method that was visible in the scope of the action, which - # returned the right stuff. - # - # That sounds attractive, but adds complications to all sorts of - # things, especially when you think about how to pass options when you - # are writing Ruby code that calls multiple faces. Especially if - # faces are involved in that. ;) - # - # --daniel 2011-04-27 - if (arity = @action.positional_arg_count) > 0 - unless (count = arguments.length) == arity then - raise ArgumentError, "wrong number of arguments (#{count} for #{arity})" - end - end - - result = @face.send(@action.name, *arguments) - puts render(result) unless result.nil? - status = true - rescue Exception => detail - puts detail.backtrace if Puppet[:trace] - - case detail - when ArgumentError then - got, want = /\((\d+) for (\d+)\)/.match(detail.to_s).to_a.map {|x| x.to_i } - Puppet.err "puppet #{@face.name} #{@action.name}: #{want} argument expected but #{got} given" + # We need to do arity checking here because this is generic code + # calling generic methods – that have argument defaulting. We need to + # make sure we don't accidentally pass the options as the first + # argument to a method that takes one argument. eg: + # + # puppet facts find + # => options => {} + # @arguments => [{}] + # => @face.send :bar, {} + # + # def face.bar(argument, options = {}) + # => bar({}, {}) # oops! we thought the options were the + # # positional argument!! + # + # We could also fix this by making it mandatory to pass the options on + # every call, but that would make the Ruby API much more annoying to + # work with; having the defaulting is a much nicer convention to have. + # + # We could also pass the arguments implicitly, by having a magic + # 'options' method that was visible in the scope of the action, which + # returned the right stuff. + # + # That sounds attractive, but adds complications to all sorts of + # things, especially when you think about how to pass options when you + # are writing Ruby code that calls multiple faces. Especially if + # faces are involved in that. ;) + # + # --daniel 2011-04-27 + if (arity = @action.positional_arg_count) > 0 + unless (count = arguments.length) == arity then + Puppet.err "puppet #{@face.name} #{@action.name}: #{arity - 1} argument expected but #{count - 1} given" Puppet.err "Try 'puppet help #{@face.name} #{@action.name}' for usage" - - else # generic exception handling, alas. + end + else + begin + result = @face.send(@action.name, *arguments) + puts render(result) unless result.nil? + status = true + rescue Exception => detail + puts detail.backtrace if Puppet[:trace] Puppet.err detail.to_s end end |
