class Sass::SyntaxError
with the information filled in.
When doing this, be sure that all exceptions ultimately end up
and then calling {#modify_backtrace} in a wrapper ‘rescue`.
by omitting the information on the original exception,
In those situations, the default values can be used
It may also be useful to have a default line number set.
the same filename or even line.
Often, a chunk of code will all have similar backtrace information -
which is called when an exception is raised between files (e.g. with `@import`).
New backtrace entries can be added with {#add_backtrace},
Some of this information is usually provided as part of the constructor.
see {#sass_backtrace} for details.
This will be used in various error reports to users, including backtraces;
it’s important to provide filename and line number information.
When dealing with SyntaxErrors,
All Sass errors are raised as {Sass::SyntaxError}s.
and the Sass file that was being parsed (if applicable).
the line of the Sass template it was raised on
An exception class that keeps track of
def add_backtrace(attrs)
-
attrs
({Symbol => Object}
) -- The information in the backtrace entry.
def add_backtrace(attrs) sass_backtrace << attrs.reject {|_k, v| v.nil?} end
def backtrace
-
(Array
-)
def backtrace return nil if super.nil? return super if sass_backtrace.all? {|h| h.empty?} sass_backtrace.map do |h| "#{h[:filename] || '(sass)'}:#{h[:line]}" + (h[:mixin] ? ":in `#{h[:mixin]}'" : "") end + super end
def exception_to_css(e, line_offset = 1)
-
(Exception)
- `e`, if the
Returns:
-
(String)
- The error report
Parameters:
-
line_offset
(Integer
) -- The number of the first line of the Sass template. -
e
(Exception
) --
def exception_to_css(e, line_offset = 1) header = header_string(e, line_offset) <<END er.gsub('*/', '*\\/')} ace:\n#{e.backtrace.join("\n").gsub('*/', '*\\/')} efore { e-space: pre; -family: monospace; ent: "#{header.gsub('"', '\"').gsub("\n", '\\A ')}"; } end
def header_string(e, line_offset)
def header_string(e, line_offset) unless e.is_a?(Sass::SyntaxError) && e.sass_line && e.sass_template return "#{e.class}: #{e.message}" end line_num = e.sass_line + 1 - line_offset min = [line_num - 6, 0].max section = e.sass_template.rstrip.split("\n")[min...line_num + 5] return e.sass_backtrace_str if section.nil? || section.empty? e.sass_backtrace_str + "\n\n" + section.each_with_index. map {|line, i| "#{line_offset + min + i}: #{line}"}.join("\n") end
def initialize(msg, attrs = {})
-
attrs
({Symbol => Object}
) -- The information in the backtrace entry. -
msg
(String
) -- The error message
def initialize(msg, attrs = {}) @message = msg @sass_backtrace = [] add_backtrace(attrs) end
def modify_backtrace(attrs)
-
attrs
({Symbol => Object}
) -- The information to add to the backtrace entry.
def modify_backtrace(attrs) attrs = attrs.reject {|_k, v| v.nil?} # Move backwards through the backtrace (0...sass_backtrace.size).to_a.reverse_each do |i| entry = sass_backtrace[i] sass_backtrace[i] = attrs.merge(entry) attrs.reject! {|k, _v| entry.include?(k)} break if attrs.empty? end end
def sass_backtrace_str(default_filename = "an unknown file")
-
(String)
-
Other tags:
- See: #sass_backtrace -
Parameters:
-
default_filename
(String
) -- The filename to use for unknown files
def sass_backtrace_str(default_filename = "an unknown file") lines = message.split("\n") msg = lines[0] + lines[1..-1]. map {|l| "\n" + (" " * "Error: ".size) + l}.join "Error: #{msg}" + sass_backtrace.each_with_index.map do |entry, i| "\n #{i == 0 ? 'on' : 'from'} line #{entry[:line]}" + " of #{entry[:filename] || default_filename}" + (entry[:mixin] ? ", in `#{entry[:mixin]}'" : "") end.join end
def sass_filename
-
(String, nil)
-
def sass_filename sass_backtrace.first[:filename] end
def sass_line
-
(Integer)
-
def sass_line sass_backtrace.first[:line] end
def sass_mixin
-
(String)
-
def sass_mixin sass_backtrace.first[:mixin] end
def to_s
-
(String)
- The error message
def to_s @message end