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) ⇒ TemplateError

Returns a new instance of TemplateError.



55
56
57
# File 'lib/chef/mixin/template.rb', line 55

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



52
53
54
# File 'lib/chef/mixin/template.rb', line 52

def context
  @context
end

#original_exceptionObject (readonly)

Returns the value of attribute original_exception.



52
53
54
# File 'lib/chef/mixin/template.rb', line 52

def original_exception
  @original_exception
end

Instance Method Details

#line_numberObject



63
64
65
# File 'lib/chef/mixin/template.rb', line 63

def line_number
  @line_number ||= $1.to_i if original_exception.backtrace.find {|line| line =~ /\(erubis\):(\d+)/ }
end

#messageObject



59
60
61
# File 'lib/chef/mixin/template.rb', line 59

def message
  @original_exception.message
end

#source_listingObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chef/mixin/template.rb', line 71

def source_listing
  @source_listing ||= begin
    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
    lines = @template.split(/\n/)
    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



67
68
69
# File 'lib/chef/mixin/template.rb', line 67

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

#to_sObject



87
88
89
90
# File 'lib/chef/mixin/template.rb', line 87

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