Sha256: 58793f155fdca4a83851b187dd90897320c79351260ef33c47a45223109c13a2

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

require 'facet/string/demodulize'

module Nitro

class ActionCompileError < SyntaxError
	SOURCE_CODE_RADIUS = 5

	attr_accessor :original_excpetion
		
	def initialize(source_code, filename, original_exception)
		@source_code = source_code.split("\n")
		@filename = filename
		@original_exception = original_exception
	end
	
	def line_number
		trace = @original_exception.backtrace.join
    
		if trace.include?(":in `class_eval'")
    	trace.scan(/:([0-9]*):in `class_eval'/).first.first.to_i
    else
    	1
    end
	end
	
  def source_extract(indent = 0)
  	ln = line_number
    start_line = [ln - SOURCE_CODE_RADIUS, 0].max
    end_line = [ln + SOURCE_CODE_RADIUS - 1, @source_code.length].min

    number = start_line
    extract = @source_code[start_line..end_line].collect do |line| 
      number += 1
      line = line.gsub(/; @out << %\^/, ' ?>').gsub(/\^;/, '<?r ')
      if number == line_number
	      "#{' ' * indent}#{number}: #{line}"
	    else
	      "#{' ' * indent}#{number}: #{line}"
	    end
    end

    return extract.join("\n")
  end

	def backtrace	
		@original_exception.backtrace.collect do |line|
#			unless line =~ /\/usr/
				line.gsub("#{Nitro::LibPath}/", '')
#			else 
#				nil
#			end
		end.compact
	end

	def to_s
		"#{self.class.to_s.demodulize} (#{@original_exception.class}): '#@filename' at line ##{line_number}"
	end
	
	def message
		"#{to_s}\n#{source_extract}"
	end
end
	
class TemplateCompileError < ActionCompileError
end

end

# * George Moschovitis <gm@navel.gr>

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nitro-0.18.0 lib/nitro/errors.rb
nitro-0.18.1 lib/nitro/errors.rb
nitro-0.19.0 lib/nitro/errors.rb
nitro-0.20.0 lib/nitro/errors.rb