Class: Sass::SyntaxError

### Home Classes Methods

### In Files

- lib/sass/error.rb

### Parent

StandardError

### Methods

- ::exception_to_css
- ::new
- #add_backtrace
- #backtrace
- #modify_backtrace
- #sass_backtrace_str
- #sass_filename
- #sass_line
- #sass_mixin
- #to_s

### Class/Module Index

Quicksearch

- Sass
- Sass::CSS
- Sass::CacheStores
- Sass::CacheStores::Base
- Sass::CacheStores::Chain
- Sass::CacheStores::Filesystem
- Sass::CacheStores::Memory
- Sass::CacheStores::Null
- Sass::Callbacks
- Sass::Callbacks::InstanceMethods
- Sass::Engine
- Sass::Engine::Line
- Sass::Environment
- Sass::Exec
- Sass::Exec::Generic
- Sass::Exec::Sass
- Sass::Exec::SassConvert
- Sass::Exec::Scss
- Sass::Importers
- Sass::Importers::Base
- Sass::Importers::Filesystem
- Sass::Logger
- Sass::Logger::Base
- Sass::Logger::LogLevel
- Sass::Logger::LogLevel::ClassMethods
- Sass::Media
- Sass::Media::Query
- Sass::Media::QueryList
- Sass::Plugin
- Sass::Plugin::Compiler
- Sass::Plugin::Configuration
- Sass::Plugin::MerbBootLoader
- Sass::Plugin::Rack
- Sass::Plugin::StalenessChecker
- Sass::Repl
- Sass::SCSS
- Sass::SCSS::CssParser
- Sass::SCSS::Parser
- Sass::SCSS::RX
- Sass::SCSS::ScriptLexer
- Sass::SCSS::ScriptParser
- Sass::SCSS::StaticParser
- Sass::Script
- Sass::Script::ArgList
- Sass::Script::Bool
- Sass::Script::Color
- Sass::Script::CssLexer
- Sass::Script::CssParser
- Sass::Script::Funcall
- Sass::Script::Functions
- Sass::Script::Functions::EvaluationContext
- Sass::Script::Interpolation
- Sass::Script::Lexer
- Sass::Script::List
- Sass::Script::Literal
- Sass::Script::Node
- Sass::Script::Null
- Sass::Script::Number
- Sass::Script::Operation
- Sass::Script::Parser
- Sass::Script::String
- Sass::Script::StringInterpolation
- Sass::Script::UnaryOperation
- Sass::Script::Variable
- Sass::Selector
- Sass::Selector::AbstractSequence
- Sass::Selector::Attribute
- Sass::Selector::Class
- Sass::Selector::CommaSequence
- Sass::Selector::Element
- Sass::Selector::Id
- Sass::Selector::Interpolation
- Sass::Selector::Parent
- Sass::Selector::Placeholder
- Sass::Selector::Pseudo
- Sass::Selector::SelectorPseudoClass
- Sass::Selector::Sequence
- Sass::Selector::Simple
- Sass::Selector::SimpleSequence
- Sass::Selector::Universal
- Sass::Shared
- Sass::Supports
- Sass::Supports::Condition
- Sass::Supports::Declaration
- Sass::Supports::Interpolation
- Sass::Supports::Negation
- Sass::Supports::Operator
- Sass::SyntaxError
- Sass::Tree
- Sass::Tree::CharsetNode
- Sass::Tree::CommentNode
- Sass::Tree::ContentNode
- Sass::Tree::CssImportNode
- Sass::Tree::DebugNode
- Sass::Tree::DirectiveNode
- Sass::Tree::EachNode
- Sass::Tree::ExtendNode
- Sass::Tree::ForNode
- Sass::Tree::FunctionNode
- Sass::Tree::IfNode
- Sass::Tree::ImportNode
- Sass::Tree::MediaNode
- Sass::Tree::MixinDefNode
- Sass::Tree::MixinNode
- Sass::Tree::Node
- Sass::Tree::PropNode
- Sass::Tree::ReturnNode
- Sass::Tree::RootNode
- Sass::Tree::RuleNode
- Sass::Tree::SupportsNode
- Sass::Tree::TraceNode
- Sass::Tree::VariableNode
- Sass::Tree::Visitors
- Sass::Tree::Visitors::Base
- Sass::Tree::Visitors::CheckNesting
- Sass::Tree::Visitors::Convert
- Sass::Tree::Visitors::Cssize
- Sass::Tree::Visitors::DeepCopy
- Sass::Tree::Visitors::Extend
- Sass::Tree::Visitors::Perform
- Sass::Tree::Visitors::SetOptions
- Sass::Tree::Visitors::ToCss
- Sass::Tree::WarnNode
- Sass::Tree::WhileNode
- Sass::UnitConversionError
- Sass::Util
- Sass::Util::MultibyteStringScanner
- Sass::Util::StaticConditionalContext
- Sass::Util::SubsetMap
- Sass::Util::Test
- Sass::Version
- ActionController
- ActionController::Base
- Object

No matching classes.

# Sass::SyntaxError

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.

### 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

exception_to_css(e, options) click to toggle source

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.

    # File lib/sass/error.rb, line 161
    def exception_to_css(e, options)
      raise e unless options[:full_exception]

      header = header_string(e, options)

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

new(msg, attrs = {}) click to toggle source

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

    See \{#sass\_backtrace}

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

### Public Instance Methods

add_backtrace(attrs) click to toggle source

Adds an entry to the exception's Sass backtrace.

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

    See \{#sass\_backtrace}

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

backtrace() click to toggle source

Returns the standard exception backtrace, including the Sass backtrace.

@return [Array<String>]

    # File lib/sass/error.rb, line 126
    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

modify_backtrace(attrs) click to toggle source

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}

    # File lib/sass/error.rb, line 106
    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

sass_backtrace_str(default_filename = "an unknown file") click to toggle source

Returns a string representation of the Sass backtrace.

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

    # File lib/sass/error.rb, line 140
    def sass_backtrace_str(default_filename = "an unknown file")
      lines = self.message.split("\n")
      msg = lines[0] + lines[1..-1].
        map {|l| "\n" + (" " * "Syntax error: ".size) + l}.join
      "Syntax error: #{msg}" +
        Sass::Util.enum_with_index(sass_backtrace).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

sass_filename() click to toggle source

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

@return [String, nil]

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

sass_line() click to toggle source

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

@return [Fixnum]

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

sass_mixin() click to toggle source

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

@return [Fixnum]

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

to_s() click to toggle source

@return [String] The error message

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.