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

Private Class Methods

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