diff --git a/lib/sassc/error.rb b/lib/sassc/error.rb index 0c90b1c0..3f126e00 100644 --- a/lib/sassc/error.rb +++ b/lib/sassc/error.rb @@ -11,6 +11,8 @@ class UnsupportedValue < BaseError; end # it's important to provide filename and line number information. # This will be used in various error reports to users, including backtraces; class SyntaxError < BaseError + LINE_INFO_REGEX = /on line (\d+) of (.+)/ + def backtrace return nil if super.nil? sass_backtrace + super @@ -18,10 +20,10 @@ def backtrace # The backtrace of the error within Sass files. def sass_backtrace - line_info = message.split("\n")[1] + line_info = message.split("\n").find { |line| line.match(LINE_INFO_REGEX) } return [] unless line_info - _, line, filename = line_info.match(/on line (\d+) of (.+)/).to_a + _, line, filename = line_info.match(LINE_INFO_REGEX).to_a ["#{Pathname.getwd.join(filename)}:#{line}"] end end diff --git a/test/error_test.rb b/test/error_test.rb index 20fd0b0a..844dc886 100644 --- a/test/error_test.rb +++ b/test/error_test.rb @@ -17,6 +17,21 @@ def test_first_backtrace_is_sass expected = "#{Pathname.getwd.join(filename)}:#{line}" assert_equal expected, err.backtrace.first end + + begin + raise SassC::SyntaxError.new(<<-ERROR) +Error: no mixin named border-radius + + Backtrace: + \t#{filename}:#{line} + on line #{line} of #{filename} +>> @include border-radius(5px); + -------------^ + ERROR + rescue SassC::SyntaxError => err + expected = "#{Pathname.getwd.join(filename)}:#{line}" + assert_equal expected, err.backtrace.first + end end end end