class Puppet::Pops::IssueReporter

Public Class Methods

assert_and_report(acceptor, options) click to toggle source

@param acceptor [Validation::Acceptor] the acceptor containing reported issues @option options [String] :message (nil) A message text to use as prefix in

a single Error message

@option options [Boolean] :emit_warnings (false) whether warnings should be emitted @option options [Boolean] :emit_errors (true) whether errors should be

emitted or only the given message

@option options [Exception] :exception_class (Puppet::ParseError) The exception to raise

   # File lib/puppet/pops/issue_reporter.rb
12 def self.assert_and_report(acceptor, options)
13   return unless acceptor
14 
15   max_errors       = options[:max_errors]   || Puppet[:max_errors]
16   max_warnings     = options[:max_warnings] || Puppet[:max_warnings]
17   max_deprecations = options[:max_deprecations] || (Puppet[:disable_warnings].include?('deprecations') ? 0 : Puppet[:max_deprecations])
18 
19   emit_warnings    = options[:emit_warnings] || false
20   emit_errors      = options[:emit_errors].nil? ? true : !!options[:emit_errors]
21   emit_message     = options[:message]
22   emit_exception   = options[:exception_class] || Puppet::ParseErrorWithIssue
23 
24   # If there are warnings output them
25   warnings = acceptor.warnings
26   if emit_warnings && warnings.size > 0
27     formatter = Validation::DiagnosticFormatterPuppetStyle.new
28     emitted_w = 0
29     emitted_dw = 0
30     acceptor.warnings.each do |w|
31       if w.severity == :deprecation
32         # Do *not* call Puppet.deprecation_warning it is for internal deprecation, not
33         # deprecation of constructs in manifests! (It is not designed for that purpose even if
34         # used throughout the code base).
35         #
36         log_message(:warning, formatter, w) if emitted_dw < max_deprecations
37         emitted_dw += 1
38       else
39         log_message(:warning, formatter, w) if emitted_w < max_warnings
40         emitted_w += 1
41       end
42       break if emitted_w >= max_warnings && emitted_dw >= max_deprecations # but only then
43     end
44   end
45 
46   # If there were errors, report the first found. Use a puppet style formatter.
47   errors = acceptor.errors
48   if errors.size > 0
49     unless emit_errors
50       raise emit_exception.new(emit_message)
51     end
52     formatter = Validation::DiagnosticFormatterPuppetStyle.new
53     if errors.size == 1 || max_errors <= 1
54       # raise immediately
55       exception = create_exception(emit_exception, emit_message, formatter, errors[0])
56       # if an exception was given as cause, use it's backtrace instead of the one indicating "here"
57       if errors[0].exception
58         exception.set_backtrace(errors[0].exception.backtrace)
59       end
60       raise exception
61     end
62     emitted = 0
63     if emit_message
64       Puppet.err(emit_message)
65     end
66     errors.each do |e|
67       log_message(:err, formatter, e)
68       emitted += 1
69       break if emitted >= max_errors
70     end
71     giving_up_message = if (emit_warnings && warnings.size > 0)
72                           _("Language validation logged %{error_count} errors, and %{warning_count} warnings. Giving up") %
73                               { error_count: errors.size, warning_count: warnings.size }
74                         else
75                           _("Language validation logged %{error_count} errors. Giving up") %
76                               { error_count: errors.size }
77                         end
78     exception = emit_exception.new(giving_up_message)
79     exception.file = errors[0].file
80     raise exception
81   end
82 end
error(exception_class, semantic, issue, args) click to toggle source
    # File lib/puppet/pops/issue_reporter.rb
101 def self.error(exception_class, semantic, issue, args)
102   raise exception_class.new(issue.format(args), semantic.file, semantic.line, semantic.pos, nil, issue.issue_code, args)
103 end
format_with_prefix(prefix, message) click to toggle source
   # File lib/puppet/pops/issue_reporter.rb
84 def self.format_with_prefix(prefix, message)
85   return message unless prefix
86   [prefix, message].join(' ')
87 end
warning(semantic, issue, args) click to toggle source
   # File lib/puppet/pops/issue_reporter.rb
89 def self.warning(semantic, issue, args)
90   Puppet::Util::Log.create({
91     :level => :warning,
92     :message => issue.format(args),
93     :arguments => args,
94     :issue_code => issue.issue_code,
95     :file => semantic.file,
96     :line => semantic.line,
97     :pos => semantic.pos,
98   })
99 end

Private Class Methods

create_exception(exception_class, emit_message, formatter, diagnostic) click to toggle source
    # File lib/puppet/pops/issue_reporter.rb
105 def self.create_exception(exception_class, emit_message, formatter, diagnostic)
106   file = diagnostic.file
107   file = (file.is_a?(String) && file.empty?) ? nil : file
108   line = pos = nil
109   if diagnostic.source_pos
110     line = diagnostic.source_pos.line
111     pos = diagnostic.source_pos.pos
112   end
113   exception_class.new(format_with_prefix(emit_message, formatter.format_message(diagnostic)), file, line, pos, nil, diagnostic.issue.issue_code, diagnostic.arguments)
114 end
log_message(severity, formatter, diagnostic) click to toggle source
    # File lib/puppet/pops/issue_reporter.rb
117 def self.log_message(severity, formatter, diagnostic)
118   file = diagnostic.file
119   file = (file.is_a?(String) && file.empty?) ? nil : file
120   line = pos = nil
121   if diagnostic.source_pos
122     line = diagnostic.source_pos.line
123     pos = diagnostic.source_pos.pos
124   end
125   Puppet::Util::Log.create({
126       :level => severity,
127       :message => formatter.format_message(diagnostic),
128       :arguments => diagnostic.arguments,
129       :issue_code => diagnostic.issue.issue_code,
130       :file => file,
131       :line => line,
132       :pos => pos,
133     })
134 end