Exception: ActionView::Template::Error

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

Overview

The Template::Error exception is raised when the compilation or rendering of the template fails. This exception then gathers a bunch of intimate details and uses it to report a precise exception message.

Constant Summary collapse

SOURCE_CODE_RADIUS =

:nodoc:

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, assigns, original_exception) ⇒ Error

Returns a new instance of Error.



59
60
61
62
63
# File 'lib/action_view/template/error.rb', line 59

def initialize(template, assigns, original_exception)
  @template, @assigns, @original_exception = template, assigns.dup, original_exception
  @sub_templates = nil
  @backtrace = original_exception.backtrace
end

Instance Attribute Details

#backtraceObject (readonly)

Returns the value of attribute backtrace.



57
58
59
# File 'lib/action_view/template/error.rb', line 57

def backtrace
  @backtrace
end

#original_exceptionObject (readonly)

Returns the value of attribute original_exception.



57
58
59
# File 'lib/action_view/template/error.rb', line 57

def original_exception
  @original_exception
end

Instance Method Details

#annoted_source_codeObject



114
115
116
# File 'lib/action_view/template/error.rb', line 114

def annoted_source_code
  source_extract(4)
end

#file_nameObject



65
66
67
# File 'lib/action_view/template/error.rb', line 65

def file_name
  @template.identifier
end

#line_numberObject



106
107
108
109
110
111
112
# File 'lib/action_view/template/error.rb', line 106

def line_number
  @line_number ||=
    if file_name
      regexp = /#{Regexp.escape File.basename(file_name)}:(\d+)/
      $1 if message =~ regexp || backtrace.find { |line| line =~ regexp }
    end
end

#messageObject



69
70
71
# File 'lib/action_view/template/error.rb', line 69

def message
  original_exception.message
end

#source_extract(indentation = 0) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/action_view/template/error.rb', line 82

def source_extract(indentation = 0)
  return unless num = line_number
  num = num.to_i

  source_code = @template.source.split("\n")

  start_on_line = [ num - SOURCE_CODE_RADIUS - 1, 0 ].max
  end_on_line   = [ num + SOURCE_CODE_RADIUS - 1, source_code.length].min

  indent = ' ' * indentation
  line_counter = start_on_line
  return unless source_code = source_code[start_on_line..end_on_line]

  source_code.sum do |line|
    line_counter += 1
    "#{indent}#{line_counter}: #{line}\n"
  end
end

#sub_template_messageObject



73
74
75
76
77
78
79
80
# File 'lib/action_view/template/error.rb', line 73

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

#sub_template_of(template_path) ⇒ Object



101
102
103
104
# File 'lib/action_view/template/error.rb', line 101

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