summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-04-28 00:04:00 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-04-28 10:51:05 -0700
commit632a0a0042ebf2e7ef209ce30005833ccee6e77b (patch)
tree124ebc6761561c0d94f9a314e1a169aae90a8355 /lib/puppet
parent80adaea6319b2aca81fd3d9b1d0061152b6c7db2 (diff)
downloadpuppet-632a0a0042ebf2e7ef209ce30005833ccee6e77b.tar.gz
puppet-632a0a0042ebf2e7ef209ce30005833ccee6e77b.tar.xz
puppet-632a0a0042ebf2e7ef209ce30005833ccee6e77b.zip
(#7269) Better error reporting for bad render formats.
Previously we would try and send `nil` to a class to render an unsupported format, which was bad. Worse, we would only discover this *after* the fact, when we tried to render, so the entire action had run and the result was lost to the world. Instead, validate the parameter early and fail during option parsing. This has less nice error reporting than we can get handling it later[1], but it gets us a much better overall set of behaviour. [1] puppet/application.rb will print and exit, rather than raising, when the option handler fails; this will improve when we unify face and application options properly. Reviewed-By: Max Martin <max@puppetlabs.com>
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/application/face_base.rb20
1 files changed, 10 insertions, 10 deletions
diff --git a/lib/puppet/application/face_base.rb b/lib/puppet/application/face_base.rb
index cc62257c7..d28a8af3b 100644
--- a/lib/puppet/application/face_base.rb
+++ b/lib/puppet/application/face_base.rb
@@ -15,8 +15,14 @@ class Puppet::Application::FaceBase < Puppet::Application
Puppet::Util::Log.level = :info
end
- option("--render-as FORMAT") do |arg|
- @render_as = arg.to_sym
+ option("--render-as FORMAT") do |_arg|
+ format = _arg.to_sym
+ unless @render_method = Puppet::Network::FormatHandler.format(format)
+ unless format == :for_humans or format == :json
+ raise ArgumentError, "I don't know how to render '#{format}'"
+ end
+ end
+ @render_as = format
end
option("--mode RUNMODE", "-r") do |arg|
@@ -36,18 +42,12 @@ class Puppet::Application::FaceBase < Puppet::Application
result = hook.call(result)
end
- begin
- render_method = Puppet::Network::FormatHandler.format(format).render_method
- rescue
- render_method = nil
- end
-
if format == :for_humans then
render_for_humans(result)
- elsif format == :json or render_method == "to_pson"
+ elsif format == :json or @render_method == "to_pson"
PSON::pretty_generate(result, :allow_nan => true, :max_nesting => false)
else
- result.send(render_method)
+ result.send(@render_method)
end
end