2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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"
|
2016-07-24 18:31:35 -04:00
|
|
|
elsif /layouts/i.match?(path)
|
2016-08-06 12:48:35 -04:00
|
|
|
"layout"
|
2010-03-19 14:49:53 -04:00
|
|
|
else
|
2016-08-06 12:48:35 -04:00
|
|
|
"template"
|
2010-03-19 14:49:53 -04:00
|
|
|
end
|
|
|
|
|
2013-11-22 08:14:41 -05:00
|
|
|
if partial && path.present?
|
|
|
|
path = path.sub(%r{([^/]+)$}, "_\\1")
|
|
|
|
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
|
|
|
|
2015-12-23 10:14:12 -05:00
|
|
|
# Override to prevent #cause resetting during re-raise.
|
|
|
|
attr_reader :cause
|
|
|
|
|
2016-10-10 01:20:11 -04:00
|
|
|
def initialize(template)
|
2015-11-03 09:54:34 -05:00
|
|
|
super($!.message)
|
|
|
|
set_backtrace($!.backtrace)
|
2015-12-23 10:14:12 -05:00
|
|
|
@cause = $!
|
2015-11-03 09:54:34 -05:00
|
|
|
@template, @sub_templates = template, nil
|
|
|
|
end
|
2004-11-23 20:04:44 -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: " +
|
2014-10-27 12:28:53 -04:00
|
|
|
@sub_templates.collect(&:inspect).join(", ")
|
2009-12-02 23:01:01 -05:00
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2019-06-21 15:04:28 -04:00
|
|
|
def source_extract(indentation = 0)
|
|
|
|
return [] unless num = line_number
|
2009-12-02 23:01:01 -05:00
|
|
|
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
|
2019-06-21 15:04:28 -04:00
|
|
|
return [] unless source_code = source_code[start_on_line..end_on_line]
|
2008-09-20 23:57:45 -04:00
|
|
|
|
2019-06-21 15:04:28 -04:00
|
|
|
formatted_code_for(source_code, start_on_line, indent)
|
2012-12-31 14:36:23 -05:00
|
|
|
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
|
2019-04-01 19:29:03 -04:00
|
|
|
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
|
|
|
|
2019-03-29 11:12:35 -04:00
|
|
|
def annotated_source_code
|
2010-05-09 05:52:26 -04:00
|
|
|
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
|
2016-08-06 12:48:35 -04:00
|
|
|
"in "
|
2009-12-02 23:01:01 -05:00
|
|
|
end + file_name
|
|
|
|
end
|
2012-12-31 18:07:21 -05:00
|
|
|
|
2019-06-21 15:04:28 -04:00
|
|
|
def formatted_code_for(source_code, line_counter, indent)
|
|
|
|
indent_template = "%#{indent}s: %s"
|
|
|
|
source_code.map do |line|
|
2012-12-31 18:07:21 -05:00
|
|
|
line_counter += 1
|
2019-06-21 15:04:28 -04:00
|
|
|
indent_template % [line_counter, line]
|
2012-12-31 18:07:21 -05:00
|
|
|
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
|
2019-02-17 23:07:16 -05:00
|
|
|
|
|
|
|
class SyntaxErrorInTemplate < TemplateError #:nodoc
|
|
|
|
def initialize(template, offending_code_string)
|
|
|
|
@offending_code_string = offending_code_string
|
|
|
|
super(template)
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
<<~MESSAGE
|
|
|
|
Encountered a syntax error while rendering template: check #{@offending_code_string}
|
|
|
|
MESSAGE
|
|
|
|
end
|
|
|
|
|
2019-03-29 11:12:35 -04:00
|
|
|
def annotated_source_code
|
2019-02-17 23:07:16 -05:00
|
|
|
@offending_code_string.split("\n").map.with_index(1) { |line, index|
|
|
|
|
indentation = " " * 4
|
|
|
|
"#{index}:#{indentation}#{line}"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2009-12-10 16:07:22 -05:00
|
|
|
end
|