Exception: Sass::SyntaxError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/sass/error.rb

Overview

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 SyntaxErrors.

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.

Direct Known Subclasses

UnitConversionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg, attrs = {}) ⇒ SyntaxError

Returns a new instance of SyntaxError.

Parameters:

  • msg (String)

    The error message

  • attrs (Hash<Symbol, Object>) (defaults to: {})

    The information in the backtrace entry. See #sass_backtrace



51
52
53
54
55
# File 'lib/sass/error.rb', line 51

def initialize(msg, attrs = {})
  @message = msg
  @sass_backtrace = []
  add_backtrace(attrs)
end

Instance Attribute Details

#sass_backtraceAray<Hash<Symbol, Object>>

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.

: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.

Returns:

  • (Aray<Hash<Symbol, Object>>)


41
42
43
# File 'lib/sass/error.rb', line 41

def sass_backtrace
  @sass_backtrace
end

#sass_templateString

The text of the template where this error was raised.

Returns:

  • (String)


46
47
48
# File 'lib/sass/error.rb', line 46

def sass_template
  @sass_template
end

Class Method Details

.exception_to_css(e, options) ⇒ Object

Returns an error report for an exception in CSS format.

Parameters:



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/sass/error.rb', line 124

def exception_to_css(e, options)
  return "/* Internal stylesheet error */" unless options[:full_exception]

  header = header_string(e, options)

  <<END
/*
#{header}

Backtrace:\n#{e.backtrace.join("\n")}
*/
body:before {
  white-space: pre;
  font-family: monospace;
  content: "#{header.gsub('"', '\"').gsub("\n", '\\A ')}"; }
END
end

Instance Method Details

#add_backtrace(attrs) ⇒ Object

Adds an entry to the exception's Sass backtrace.

Parameters:



76
77
78
# File 'lib/sass/error.rb', line 76

def add_backtrace(attrs)
  sass_backtrace << attrs.reject {|k, v| v.nil?}
end

#backtraceArray<String>

Returns the standard exception backtrace, including the Sass backtrace.

Returns:

  • (Array<String>)


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

def backtrace
  return nil if super.nil?
  sass_backtrace.map {|h| "#{h[:filename] || "(sass)"}:#{h[:line]}"} + super
end

#modify_backtrace(attrs) ⇒ Object

Modify the top Sass backtrace entry (that is, the last one) to have the given attributes. If that entry already has one of the given attributes set, that takes precendence.

Parameters:



87
88
89
# File 'lib/sass/error.rb', line 87

def modify_backtrace(attrs)
  sass_backtrace[-1] = attrs.reject {|k, v| v.nil?}.merge(sass_backtrace.last)
end

#sass_backtrace_str(default_filename = "an unknown file") ⇒ String

Returns a string representation of the Sass backtrace.

Parameters:

  • default_filename (String) (defaults to: "an unknown file")

    The filename to use for unknown files

Returns:

  • (String)

See Also:



110
111
112
113
114
115
116
# File 'lib/sass/error.rb', line 110

def sass_backtrace_str(default_filename = "an unknown file")
  "Syntax error: #{message}" +
    Haml::Util.enum_with_index(sass_backtrace).map do |entry, i|
    "\n        #{i == 0 ? "on" : "from"} line #{entry[:line]}" +
      " of #{entry[:filename] || default_filename}"
  end.join
end

#sass_filenameString

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

Returns:

  • (String)


61
62
63
# File 'lib/sass/error.rb', line 61

def sass_filename
  sass_backtrace.first[:filename]
end

#sass_lineFixnum

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

Returns:

  • (Fixnum)


68
69
70
# File 'lib/sass/error.rb', line 68

def sass_line
  sass_backtrace.first[:line]
end

#to_sString

Returns The error message.

Returns:

  • (String)

    The error message



92
93
94
# File 'lib/sass/error.rb', line 92

def to_s
  @message
end