Sha256: 3d6fdd99d7d700ca77ff03a4628644b5decd13c768a569aceeab527fa8d2f7b9

Contents?: true

Size: 1.4 KB

Versions: 8

Compression:

Stored size: 1.4 KB

Contents

module Sprinkle::Errors
  # Blatantly stole this from Chef
  class TemplateError < RuntimeError #:nodoc:
    attr_reader :original_exception, :context
    SOURCE_CONTEXT_WINDOW = 2 unless defined? SOURCE_CONTEXT_WINDOW

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

    def message
      @original_exception.message
    end

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

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

    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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sprinkle-0.7.7 lib/sprinkle/errors/template_error.rb
sprinkle-0.7.6.2 lib/sprinkle/errors/template_error.rb
sprinkle-0.7.6.1 lib/sprinkle/errors/template_error.rb
sprinkle-0.7.6 lib/sprinkle/errors/template_error.rb
sprinkle-0.7.5 lib/sprinkle/errors/template_error.rb
sprinkle-0.7.4 lib/sprinkle/errors/template_error.rb
sprinkle-0.7.3 lib/sprinkle/errors/template_error.rb
sprinkle-0.7.2 lib/sprinkle/errors/template_error.rb