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) ⇒ Error

Returns a new instance of Error.



162
163
164
165
166
167
168
169
# File 'lib/action_view/template/error.rb', line 162

def initialize(template)
  super($!.message)
  @cause = $!
  if @cause.is_a?(SyntaxError)
    @cause = ActiveSupport::SyntaxErrorProxy.new(@cause)
  end
  @template, @sub_templates = template, nil
end

Instance Attribute Details

#causeObject (readonly)

Override to prevent #cause resetting during re-raise.



158
159
160
# File 'lib/action_view/template/error.rb', line 158

def cause
  @cause
end

#templateObject (readonly)

Returns the value of attribute template.



160
161
162
# File 'lib/action_view/template/error.rb', line 160

def template
  @template
end

Instance Method Details

#annotated_source_codeObject



220
221
222
# File 'lib/action_view/template/error.rb', line 220

def annotated_source_code
  source_extract(4)
end

#backtraceObject



171
172
173
# File 'lib/action_view/template/error.rb', line 171

def backtrace
  @cause.backtrace
end

#backtrace_locationsObject



175
176
177
# File 'lib/action_view/template/error.rb', line 175

def backtrace_locations
  @cause.backtrace_locations
end

#file_nameObject



179
180
181
# File 'lib/action_view/template/error.rb', line 179

def file_name
  @template.identifier
end

#line_numberObject



212
213
214
215
216
217
218
# File 'lib/action_view/template/error.rb', line 212

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

#source_extract(indentation = 0) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/action_view/template/error.rb', line 192

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

  source_code = @template.encode!.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 = end_on_line.to_s.size + indentation
  return [] unless source_code = source_code[start_on_line..end_on_line]

  formatted_code_for(source_code, start_on_line, indent)
end

#sub_template_messageObject



183
184
185
186
187
188
189
190
# File 'lib/action_view/template/error.rb', line 183

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

#sub_template_of(template_path) ⇒ Object



207
208
209
210
# File 'lib/action_view/template/error.rb', line 207

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