Exception: Chef::Mixin::Template::TemplateError

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/chef/mixin/template.rb

Constant Summary collapse

SOURCE_CONTEXT_WINDOW =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_exception, template, context, options) ⇒ TemplateError

Returns a new instance of TemplateError.



218
219
220
# File 'lib/chef/mixin/template.rb', line 218

def initialize(original_exception, template, context, options)
  @original_exception, @template, @context, @options = original_exception, template, context, options
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



215
216
217
# File 'lib/chef/mixin/template.rb', line 215

def context
  @context
end

#optionsObject (readonly)

Returns the value of attribute options.



215
216
217
# File 'lib/chef/mixin/template.rb', line 215

def options
  @options
end

#original_exceptionObject (readonly)

Returns the value of attribute original_exception.



215
216
217
# File 'lib/chef/mixin/template.rb', line 215

def original_exception
  @original_exception
end

Instance Method Details

#line_numberObject



226
227
228
229
230
231
232
# File 'lib/chef/mixin/template.rb', line 226

def line_number
  @line_number ||= if options[:filename]
                     $1.to_i if original_exception.backtrace.find { |line| line =~ /#{Regexp.escape(options[:filename])}:(\d+)/ }
                   else
                     $1.to_i if original_exception.backtrace.find { |line| line =~ /\(erubis\):(\d+)/ }
                   end
end

#messageObject



222
223
224
# File 'lib/chef/mixin/template.rb', line 222

def message
  @original_exception.message
end

#source_listingObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/chef/mixin/template.rb', line 238

def source_listing
  @source_listing ||= begin
    lines = @template.split(/\n/)
    if line_number
      line_index = line_number - 1
      beginning_line = line_index <= SOURCE_CONTEXT_WINDOW ? 0 : line_index - SOURCE_CONTEXT_WINDOW
      source_size = SOURCE_CONTEXT_WINDOW * 2 + 1
    else
      beginning_line = 0
      source_size    = lines.length
    end
    contextual_lines = lines[beginning_line, source_size]
    output = []
    contextual_lines.each_with_index do |line, index|
      line_number = (index + beginning_line + 1).to_s.rjust(3)
      output << "#{line_number}: #{line}"
    end
    output.join("\n")
  end
end

#source_locationObject



234
235
236
# File 'lib/chef/mixin/template.rb', line 234

def source_location
  "on line ##{line_number}"
end

#to_sObject



259
260
261
262
# File 'lib/chef/mixin/template.rb', line 259

def to_s
  "\n\n#{self.class} (#{message}) #{source_location}:\n\n" +
    "#{source_listing}\n\n  #{original_exception.backtrace.join("\n  ")}\n\n"
end