Exception: ActionView::TemplateError

Inherits:
ActionViewError show all
Defined in:
lib/action_view/template_error.rb

Overview

The TemplateError exception is raised when the compilation of the template fails. This exception then gathers a bunch of intimate details and uses it to report a very precise exception message.

Constant Summary collapse

SOURCE_CODE_RADIUS =

:nodoc:

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path, file_name, assigns, source, original_exception) ⇒ TemplateError

Returns a new instance of TemplateError.



9
10
11
12
# File 'lib/action_view/template_error.rb', line 9

def initialize(base_path, file_name, assigns, source, original_exception)
  @base_path, @file_name, @assigns, @source, @original_exception = 
    base_path, file_name, assigns, source, original_exception
end

Instance Attribute Details

#original_exceptionObject (readonly)

Returns the value of attribute original_exception.



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

def original_exception
  @original_exception
end

Instance Method Details

#backtraceObject



73
74
75
76
77
78
# File 'lib/action_view/template_error.rb', line 73

def backtrace
  [ 
    "On line ##{line_number} of #{file_name}\n\n#{source_extract(4)}\n    " + 
    clean_backtrace(original_exception).join("\n    ")
  ]
end

#file_nameObject



62
63
64
# File 'lib/action_view/template_error.rb', line 62

def file_name
  strip_base_path(@file_name)
end

#line_numberObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/action_view/template_error.rb', line 51

def line_number
  trace = @original_exception.backtrace.join
  if trace.include?("erb):")
    trace.scan(/\((?:erb)\):([0-9]*)/).first.first.to_i
  elsif trace.include?("eval):")
    trace.scan(/\((?:eval)\):([0-9]*)/).first.first.to_i
  else
    1
  end
end

#messageObject



14
15
16
17
18
19
20
# File 'lib/action_view/template_error.rb', line 14

def message
  if original_exception.message.include?("(eval):")
    original_exception.message.scan(/\(eval\):(?:[0-9]*):in `.*'(.*)/).first.first
  else
    original_exception.message
  end
end

#source_extract(indention = 0) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/action_view/template_error.rb', line 31

def source_extract(indention = 0)
  source_code = IO.readlines(@file_name)
  
  start_on_line = [ line_number - SOURCE_CODE_RADIUS - 1, 0 ].max
  end_on_line   = [ line_number + SOURCE_CODE_RADIUS - 1, source_code.length].min

  line_counter = start_on_line
  extract = source_code[start_on_line..end_on_line].collect do |line| 
    line_counter += 1
    "#{' ' * indention}#{line_counter}: " + line
  end

  extract.join
end

#sub_template_messageObject



22
23
24
25
26
27
28
29
# File 'lib/action_view/template_error.rb', line 22

def sub_template_message
  if @sub_templates
    "Trace of template inclusion: " +
    @sub_templates.collect { |template| strip_base_path(template) }.join(", ")
  else
    ""
  end
end

#sub_template_of(file_name) ⇒ Object



46
47
48
49
# File 'lib/action_view/template_error.rb', line 46

def sub_template_of(file_name)
  @sub_templates ||= []
  @sub_templates << file_name
end

#to_sObject



66
67
68
69
70
71
# File 'lib/action_view/template_error.rb', line 66

def to_s
  "\n\n#{self.class} (#{message}) on line ##{line_number} of #{file_name}:\n" + 
  source_extract + "\n    " +
  clean_backtrace(original_exception).join("\n    ") +
  "\n\n"
end