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_path, assigns, source, original_exception) ⇒ TemplateError

Returns a new instance of TemplateError.



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

def initialize(base_path, file_path, assigns, source, original_exception)
  @base_path, @assigns, @source, @original_exception =
    base_path, assigns.dup, source, original_exception
  @file_path = file_path
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



75
76
77
78
79
80
# File 'lib/action_view/template_error.rb', line 75

def backtrace
  [
    "#{source_location.capitalize}\n\n#{source_extract(4)}\n    " +
    clean_backtrace.join("\n    ")
  ]
end

#clean_backtraceObject



19
20
21
# File 'lib/action_view/template_error.rb', line 19

def clean_backtrace
  original_exception.clean_backtrace
end

#file_nameObject



64
65
66
67
68
# File 'lib/action_view/template_error.rb', line 64

def file_name
  stripped = strip_base_path(@file_path)
  stripped.slice!(0,1) if stripped[0] == ?/
  stripped
end

#line_numberObject



55
56
57
58
59
60
61
62
# File 'lib/action_view/template_error.rb', line 55

def line_number
  @line_number ||=
    if file_name
      regexp = /#{Regexp.escape File.basename(file_name)}:(\d+)/

      $1 if message =~ regexp or clean_backtrace.find { |line| line =~ regexp }
    end
end

#messageObject



15
16
17
# File 'lib/action_view/template_error.rb', line 15

def message
  ActiveSupport::Deprecation.silence { original_exception.message }
end

#source_extract(indentation = 0) ⇒ Object



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

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

  source_code = IO.readlines(@file_path)

  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

  source_code[start_on_line..end_on_line].sum do |line|
    line_counter += 1
    "#{indent}#{line_counter}: #{line}"
  end
end

#sub_template_messageObject



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

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(template_path) ⇒ Object



50
51
52
53
# File 'lib/action_view/template_error.rb', line 50

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

#to_sObject



70
71
72
73
# File 'lib/action_view/template_error.rb', line 70

def to_s
  "\n\n#{self.class} (#{message}) #{source_location}:\n" +
    "#{source_extract}\n    #{clean_backtrace.join("\n    ")}\n\n"
end