2009-06-08 16:33:18 -04:00
|
|
|
require "active_support/core_ext/enumerable"
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
module ActionView
|
2010-06-20 16:26:31 -04:00
|
|
|
# = Action View Errors
|
2010-03-19 14:49:53 -04:00
|
|
|
class ActionViewError < StandardError #:nodoc:
|
|
|
|
end
|
|
|
|
|
2010-05-17 11:41:54 -04:00
|
|
|
class EncodingError < StandardError #:nodoc:
|
|
|
|
end
|
|
|
|
|
2012-09-01 07:58:24 -04:00
|
|
|
class MissingRequestError < StandardError #:nodoc:
|
|
|
|
end
|
|
|
|
|
2010-05-17 11:41:54 -04:00
|
|
|
class WrongEncodingError < EncodingError #:nodoc:
|
|
|
|
def initialize(string, encoding)
|
|
|
|
@string, @encoding = string, encoding
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
2013-01-28 01:23:31 -05:00
|
|
|
@string.force_encoding(Encoding::ASCII_8BIT)
|
2010-05-17 11:41:54 -04:00
|
|
|
"Your template was not saved as valid #{@encoding}. Please " \
|
|
|
|
"either specify #{@encoding} as the encoding for your template " \
|
|
|
|
"in your text editor, or mark the template with its " \
|
|
|
|
"encoding by inserting the following as the first line " \
|
|
|
|
"of the template:\n\n# encoding: <name of correct encoding>.\n\n" \
|
|
|
|
"The source of your template was:\n\n#{@string}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-19 14:49:53 -04:00
|
|
|
class MissingTemplate < ActionViewError #:nodoc:
|
|
|
|
attr_reader :path
|
|
|
|
|
2010-12-27 02:44:51 -05:00
|
|
|
def initialize(paths, path, prefixes, partial, details, *)
|
2010-03-19 14:49:53 -04:00
|
|
|
@path = path
|
2012-01-05 15:18:54 -05:00
|
|
|
prefixes = Array(prefixes)
|
2010-03-19 14:49:53 -04:00
|
|
|
template_type = if partial
|
|
|
|
"partial"
|
|
|
|
elsif path =~ /layouts/i
|
|
|
|
'layout'
|
|
|
|
else
|
|
|
|
'template'
|
|
|
|
end
|
|
|
|
|
2010-12-27 02:44:51 -05:00
|
|
|
searched_paths = prefixes.map { |prefix| [prefix, path].join("/") }
|
|
|
|
|
|
|
|
out = "Missing #{template_type} #{searched_paths.join(", ")} with #{details.inspect}. Searched in:\n"
|
|
|
|
out += paths.compact.map { |p| " * #{p.to_s.inspect}\n" }.join
|
|
|
|
super out
|
2010-03-19 14:49:53 -04:00
|
|
|
end
|
|
|
|
end
|
2010-05-03 06:54:52 -04:00
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
class Template
|
2010-09-24 17:06:47 -04:00
|
|
|
# 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.
|
2009-12-02 23:01:01 -05:00
|
|
|
class Error < ActionViewError #:nodoc:
|
|
|
|
SOURCE_CODE_RADIUS = 3
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-05-03 06:54:52 -04:00
|
|
|
attr_reader :original_exception, :backtrace
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2012-01-20 11:13:29 -05:00
|
|
|
def initialize(template, original_exception)
|
2010-09-24 21:42:01 -04:00
|
|
|
super(original_exception.message)
|
2012-01-20 11:13:29 -05:00
|
|
|
@template, @original_exception = template, original_exception
|
2010-09-28 14:33:48 -04:00
|
|
|
@sub_templates = nil
|
2010-05-03 06:54:52 -04:00
|
|
|
@backtrace = original_exception.backtrace
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
2006-11-17 07:47:08 -05:00
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
def file_name
|
|
|
|
@template.identifier
|
|
|
|
end
|
2008-09-20 23:57:45 -04:00
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
def sub_template_message
|
|
|
|
if @sub_templates
|
|
|
|
"Trace of template inclusion: " +
|
|
|
|
@sub_templates.collect { |template| template.inspect }.join(", ")
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2012-12-31 14:36:23 -05:00
|
|
|
def source_extract(indentation = 0, output = :console)
|
2009-12-02 23:01:01 -05:00
|
|
|
return unless num = line_number
|
|
|
|
num = num.to_i
|
2006-11-17 07:47:08 -05:00
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
source_code = @template.source.split("\n")
|
2006-11-17 07:47:08 -05:00
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
start_on_line = [ num - SOURCE_CODE_RADIUS - 1, 0 ].max
|
|
|
|
end_on_line = [ num + SOURCE_CODE_RADIUS - 1, source_code.length].min
|
2006-11-17 07:47:08 -05:00
|
|
|
|
2012-08-02 20:23:06 -04:00
|
|
|
indent = end_on_line.to_s.size + indentation
|
2009-12-02 23:01:01 -05:00
|
|
|
return unless source_code = source_code[start_on_line..end_on_line]
|
2008-09-20 23:57:45 -04:00
|
|
|
|
2012-12-31 14:36:23 -05:00
|
|
|
formatted_code_for(source_code, start_on_line, indent, output)
|
|
|
|
end
|
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
def sub_template_of(template_path)
|
|
|
|
@sub_templates ||= []
|
|
|
|
@sub_templates << template_path
|
|
|
|
end
|
2006-11-17 07:47:08 -05:00
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
def line_number
|
|
|
|
@line_number ||=
|
|
|
|
if file_name
|
|
|
|
regexp = /#{Regexp.escape File.basename(file_name)}:(\d+)/
|
2010-05-03 06:54:52 -04:00
|
|
|
$1 if message =~ regexp || backtrace.find { |line| line =~ regexp }
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
end
|
2004-12-13 18:28:10 -05:00
|
|
|
|
2010-05-09 05:52:26 -04:00
|
|
|
def annoted_source_code
|
|
|
|
source_extract(4)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2009-12-02 23:01:01 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def source_location
|
|
|
|
if line_number
|
|
|
|
"on line ##{line_number} of "
|
|
|
|
else
|
|
|
|
'in '
|
|
|
|
end + file_name
|
|
|
|
end
|
2012-12-31 18:07:21 -05:00
|
|
|
|
|
|
|
def formatted_code_for(source_code, line_counter, indent, output)
|
|
|
|
start_value = (output == :html) ? {} : ""
|
|
|
|
source_code.inject(start_value) do |result, line|
|
|
|
|
line_counter += 1
|
|
|
|
if output == :html
|
|
|
|
result.update(line_counter.to_s => "%#{indent}s %s\n" % ["", line])
|
|
|
|
else
|
|
|
|
result << "%#{indent}s: %s\n" % [line_counter, line]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2009-12-10 16:07:22 -05:00
|
|
|
|
|
|
|
TemplateError = Template::Error
|
|
|
|
end
|