Exception: Sass::SyntaxError
- 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
Instance Attribute Summary collapse
-
#sass_backtrace ⇒ Aray<Hash<Symbol, Object>>
The backtrace of the error within Sass files.
-
#sass_template ⇒ String
The text of the template where this error was raised.
Class Method Summary collapse
-
.exception_to_css(e, options) ⇒ Object
Returns an error report for an exception in CSS format.
Instance Method Summary collapse
-
#add_backtrace(attrs) ⇒ Object
Adds an entry to the exception's Sass backtrace.
-
#backtrace ⇒ Array<String>
Returns the standard exception backtrace, including the Sass backtrace.
-
#initialize(msg, attrs = {}) ⇒ SyntaxError
constructor
A new instance of SyntaxError.
-
#modify_backtrace(attrs) ⇒ Object
Modify the top Sass backtrace entry (that is, the last one) to have the given attributes.
-
#sass_backtrace_str(default_filename = "an unknown file") ⇒ String
Returns a string representation of the Sass backtrace.
-
#sass_filename ⇒ String
The name of the file in which the exception was raised.
-
#sass_line ⇒ Fixnum
The line of the Sass template on which the error occurred.
-
#to_s ⇒ String
The error message.
Constructor Details
#initialize(msg, attrs = {}) ⇒ SyntaxError
Returns a new instance of SyntaxError.
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_backtrace ⇒ Aray<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.
41 42 43 |
# File 'lib/sass/error.rb', line 41
def sass_backtrace
@sass_backtrace
end
|
#sass_template ⇒ String
The text of the template where this error was raised.
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.
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.
76 77 78 |
# File 'lib/sass/error.rb', line 76
def add_backtrace(attrs)
sass_backtrace << attrs.reject {|k, v| v.nil?}
end
|
#backtrace ⇒ Array<String>
Returns the standard exception backtrace, including the Sass backtrace.
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.
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.
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_filename ⇒ String
The name of the file in which the exception was raised.
This could be nil
if no filename is available.
61 62 63 |
# File 'lib/sass/error.rb', line 61
def sass_filename
sass_backtrace.first[:filename]
end
|
#sass_line ⇒ Fixnum
The line of the Sass template on which the error occurred.
68 69 70 |
# File 'lib/sass/error.rb', line 68
def sass_line
sass_backtrace.first[:line]
end
|
#to_s ⇒ String
Returns The error message.
92 93 94 |
# File 'lib/sass/error.rb', line 92
def to_s
@message
end
|