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.



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

def initialize(template)
  super($!.message)
  set_backtrace($!.backtrace)
  @cause = $!
  @template, @sub_templates = template, nil
end

Instance Attribute Details

#causeObject (readonly)

Override to prevent #cause resetting during re-raise.



63
64
65
# File 'lib/action_view/template/error.rb', line 63

def cause
  @cause
end

Instance Method Details

#annoted_source_codeObject



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

def annoted_source_code
  source_extract(4)
end

#file_nameObject



72
73
74
# File 'lib/action_view/template/error.rb', line 72

def file_name
  @template.identifier
end

#line_numberObject



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

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, output = :console) ⇒ Object



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

def source_extract(indentation = 0, output = :console)
  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 = 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, output)
end

#sub_template_messageObject



76
77
78
79
80
81
82
83
# File 'lib/action_view/template/error.rb', line 76

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



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

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