Exception: Sprinkle::Errors::TemplateError

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/sprinkle/errors/template_error.rb

Overview

Blatantly stole this from Chef

Constant Summary collapse

SOURCE_CONTEXT_WINDOW =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_exception, template, context) ⇒ TemplateError


7
8
9
# File 'lib/sprinkle/errors/template_error.rb', line 7

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

Instance Attribute Details

#contextObject (readonly)

:nodoc:


4
5
6
# File 'lib/sprinkle/errors/template_error.rb', line 4

def context
  @context
end

#original_exceptionObject (readonly)

:nodoc:


4
5
6
# File 'lib/sprinkle/errors/template_error.rb', line 4

def original_exception
  @original_exception
end

Instance Method Details

#line_numberObject


15
16
17
# File 'lib/sprinkle/errors/template_error.rb', line 15

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

#messageObject


11
12
13
# File 'lib/sprinkle/errors/template_error.rb', line 11

def message
  @original_exception.message
end

#source_listingObject


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sprinkle/errors/template_error.rb', line 23

def source_listing
  return nil if line_number.nil?

  @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


19
20
21
# File 'lib/sprinkle/errors/template_error.rb', line 19

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

#to_sObject


41
42
43
44
# File 'lib/sprinkle/errors/template_error.rb', line 41

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