Class Sass::SyntaxError
In: lib/sass/error.rb
Parent: StandardError
Haml::Util Engine Color SyntaxError UnitConversionError StandardError AbstractSequence CommaSequence Sequence SimpleSequence Simple Parent Universal Class Negation Id Pseudo Attribute Interpolation Element Node Operation Literal UnaryOperation StringInterpolation Funcall Variable Interpolation Lexer CssLexer Number String Bool Parser Parser CssParser EvaluationContext StaticParser SassParser CssParser Node DebugNode IfNode CommentNode ForNode PropNode MixinNode DirectiveNode VariableNode RootNode WarnNode ExtendNode RuleNode MixinDefNode WhileNode Enumerable ImportNode Merb::BootLoader MerbBootLoader Repl CSS Environment Rack StalenessChecker lib/sass/repl.rb lib/sass/css.rb lib/sass/environment.rb lib/sass/error.rb lib/sass/engine.rb lib/sass/selector/simple_sequence.rb lib/sass/selector/abstract_sequence.rb lib/sass/selector/sequence.rb lib/sass/selector/comma_sequence.rb lib/sass/selector/simple.rb lib/sass/selector.rb Selector lib/sass/script/css_parser.rb lib/sass/script/lexer.rb lib/sass/script/color.rb lib/sass/script/string.rb lib/sass/script/unary_operation.rb lib/sass/script/variable.rb lib/sass/script/funcall.rb lib/sass/script/string_interpolation.rb lib/sass/script/operation.rb lib/sass/script/bool.rb lib/sass/script/parser.rb lib/sass/script/node.rb lib/sass/script/literal.rb lib/sass/script/interpolation.rb lib/sass/script/css_lexer.rb lib/sass/script/number.rb lib/sass/script/functions.rb Functions Script lib/sass/scss/sass_parser.rb lib/sass/scss/static_parser.rb lib/sass/scss/parser.rb lib/sass/scss/css_parser.rb ScriptParser ScriptLexer RX SCSS Files Callbacks lib/sass/tree/while_node.rb lib/sass/tree/if_node.rb lib/sass/tree/mixin_def_node.rb lib/sass/tree/debug_node.rb lib/sass/tree/root_node.rb lib/sass/tree/for_node.rb lib/sass/tree/import_node.rb lib/sass/tree/prop_node.rb lib/sass/tree/node.rb lib/sass/tree/comment_node.rb lib/sass/tree/extend_node.rb lib/sass/tree/mixin_node.rb lib/sass/tree/warn_node.rb lib/sass/tree/directive_node.rb lib/sass/tree/rule_node.rb lib/sass/tree/variable_node.rb Tree lib/sass/plugin/rack.rb lib/sass/plugin/staleness_checker.rb lib/sass/plugin/merb.rb Plugin Sass dot/m_61_0.png

An exception class that keeps track of the line of the Sass template it was raised on and the Sass file that was being parsed (if applicable).

All Sass errors are raised as {Sass::SyntaxError}s.

When dealing with SyntaxErrors, it‘s important to provide filename and line number information. This will be used in various error reports to users, including backtraces; see \{sass_backtrace} for details.

Some of this information is usually provided as part of the constructor. New backtrace entries can be added with \{add_backtrace}, which is called when an exception is raised between files (e.g. with `@import`).

Often, a chunk of code will all have similar backtrace information - the same filename or even line. It may also be useful to have a default line number set. In those situations, the default values can be used by omitting the information on the original exception, and then calling \{modify_backtrace} in a wrapper `rescue`. When doing this, be sure that all exceptions ultimately end up with the information filled in.

Methods

Attributes

sass_backtrace  [RW]  The backtrace of the error within Sass files. This is an array of hashes containing information for a single entry. The hashes have the following keys:

`:filename` : The name of the file in which the exception was raised,

  or `nil` if no filename is available.

`:mixin` : The name of the mixin in which the exception was raised,

  or `nil` if it wasn't raised in a mixin.

`:line` : The line of the file on which the error occurred. Never nil.

This information is also included in standard backtrace format in the output of \{backtrace}.

@return [Aray<{Symbol => Object>}]

sass_template  [RW]  The text of the template where this error was raised.

@return [String]

Public Class methods

Returns an error report for an exception in CSS format.

@param e [Exception] @param options [{Symbol => Object}] The options passed to {Sass::Engine#initialize} @return [String] The error report @raise [Exception] `e`, if the

  {file:SASS_REFERENCE.md#full_exception-option `:full_exception`} option
  is set to false.

[Source]

     # File lib/sass/error.rb, line 160
160:       def exception_to_css(e, options)
161:         raise e unless options[:full_exception]
162: 
163:         header = header_string(e, options)
164: 
165:         "/*\n\#{header}\n\nBacktrace:\\n\#{e.backtrace.join(\"\\n\")}\n*/\nbody:before {\n  white-space: pre;\n  font-family: monospace;\n  content: \"\#{header.gsub('\"', '\\\"').gsub(\"\\n\", '\\\\A ')}\"; }\n"
166:       end

@param msg [String] The error message @param attrs [{Symbol => Object}] The information in the backtrace entry.

  See \{#sass\_backtrace}

[Source]

    # File lib/sass/error.rb, line 55
55:     def initialize(msg, attrs = {})
56:       @message = msg
57:       @sass_backtrace = []
58:       add_backtrace(attrs)
59:     end

Private Class methods

[Source]

     # File lib/sass/error.rb, line 181
181:       def header_string(e, options)
182:         return "#{e.class}: #{e.message}" unless e.is_a? Sass::SyntaxError
183: 
184:         line_offset = options[:line] || 1
185:         line_num = e.sass_line + 1 - line_offset
186:         min = [line_num - 6, 0].max
187:         section = e.sass_template.rstrip.split("\n")[min ... line_num + 5]
188:         return e.sass_backtrace_str if section.nil? || section.empty?
189: 
190:         e.sass_backtrace_str + "\n\n" + Haml::Util.enum_with_index(section).
191:           map {|line, i| "#{line_offset + min + i}: #{line}"}.join("\n")
192:       end

Public Instance methods

Adds an entry to the exception‘s Sass backtrace.

@param attrs [{Symbol => Object}] The information in the backtrace entry.

  See \{#sass\_backtrace}

[Source]

    # File lib/sass/error.rb, line 88
88:     def add_backtrace(attrs)
89:       sass_backtrace << attrs.reject {|k, v| v.nil?}
90:     end

Returns the standard exception backtrace, including the Sass backtrace.

@return [Array<String>]

[Source]

     # File lib/sass/error.rb, line 126
126:     def backtrace
127:       return nil if super.nil?
128:       sass_backtrace.map do |h|
129:         "#{h[:filename] || "(sass)"}:#{h[:line]}" +
130:           (h[:mixin] ? ":in `#{h[:mixin]}'" : "")
131:       end + super
132:     end

Modify the top Sass backtrace entries (that is, the most deeply nested ones) to have the given attributes.

Specifically, this goes through the backtrace entries from most deeply nested to least, setting the given attributes for each entry. If an entry already has one of the given attributes set, the pre-existing attribute takes precedence and is not used for less deeply-nested entries (even if they don‘t have that attribute set).

@param attrs [{Symbol => Object}] The information to add to the backtrace entry.

  See \{#sass\_backtrace}

[Source]

     # File lib/sass/error.rb, line 106
106:     def modify_backtrace(attrs)
107:       attrs = attrs.reject {|k, v| v.nil?}
108:       # Move backwards through the backtrace
109:       (0...sass_backtrace.size).to_a.reverse.each do |i|
110:         entry = sass_backtrace[i]
111:         sass_backtrace[i] = attrs.merge(entry)
112:         attrs.reject! {|k, v| entry.include?(k)}
113:         break if attrs.empty?
114:       end
115:     end

Returns a string representation of the Sass backtrace.

@param default_filename [String] The filename to use for unknown files @see sass_backtrace @return [String]

[Source]

     # File lib/sass/error.rb, line 139
139:     def sass_backtrace_str(default_filename = "an unknown file")
140:       lines = self.message.split("\n")
141:       msg = lines[0] + lines[1..-1].
142:         map {|l| "\n" + (" " * "Syntax error: ".size) + l}.join
143:       "Syntax error: #{msg}" +
144:         Haml::Util.enum_with_index(sass_backtrace).map do |entry, i|
145:         "\n        #{i == 0 ? "on" : "from"} line #{entry[:line]}" +
146:           " of #{entry[:filename] || default_filename}" +
147:           (entry[:mixin] ? ", in `#{entry[:mixin]}'" : "")
148:       end.join
149:     end

The name of the file in which the exception was raised. This could be `nil` if no filename is available.

@return [String, nil]

[Source]

    # File lib/sass/error.rb, line 65
65:     def sass_filename
66:       sass_backtrace.first[:filename]
67:     end

The line of the Sass template on which the error occurred.

@return [Fixnum]

[Source]

    # File lib/sass/error.rb, line 80
80:     def sass_line
81:       sass_backtrace.first[:line]
82:     end

The name of the mixin in which the error occurred. This could be `nil` if the error occurred outside a mixin.

@return [Fixnum]

[Source]

    # File lib/sass/error.rb, line 73
73:     def sass_mixin
74:       sass_backtrace.first[:mixin]
75:     end

@return [String] The error message

[Source]

     # File lib/sass/error.rb, line 118
118:     def to_s
119:       @message
120:     end

[Validate]