From 607f6619c1fc3a0773805a5b030ce19fd077857c Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Fri, 15 Jan 2016 18:46:37 +0900 Subject: [PATCH 1/2] Show sass file on the top of backtrace --- lib/sassc/error.rb | 21 ++++++++++++++++++++- test/error_test.rb | 22 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 test/error_test.rb diff --git a/lib/sassc/error.rb b/lib/sassc/error.rb index c508c8d9..0c90b1c0 100644 --- a/lib/sassc/error.rb +++ b/lib/sassc/error.rb @@ -1,9 +1,28 @@ +require 'pathname' require 'sass/error' module SassC class BaseError < StandardError; end - class SyntaxError < BaseError; end class NotRenderedError < BaseError; end class InvalidStyleError < BaseError; end class UnsupportedValue < BaseError; end + + # 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; + class SyntaxError < BaseError + def backtrace + return nil if super.nil? + sass_backtrace + super + end + + # The backtrace of the error within Sass files. + def sass_backtrace + line_info = message.split("\n")[1] + return [] unless line_info + + _, line, filename = line_info.match(/on line (\d+) of (.+)/).to_a + ["#{Pathname.getwd.join(filename)}:#{line}"] + end + end end diff --git a/test/error_test.rb b/test/error_test.rb new file mode 100644 index 00000000..20fd0b0a --- /dev/null +++ b/test/error_test.rb @@ -0,0 +1,22 @@ +require_relative "test_helper" + +module SassC + class ErrorTest < MiniTest::Test + def test_first_backtrace_is_sass + line = 2 + filename = "app/assets/stylesheets/application.scss" + + begin + raise SassC::SyntaxError.new(<<-ERROR) +Error: property "padding" must be followed by a ':' + on line #{line} of #{filename} +>> padding top: 10px; + --^ + ERROR + rescue SassC::SyntaxError => err + expected = "#{Pathname.getwd.join(filename)}:#{line}" + assert_equal expected, err.backtrace.first + end + end + end +end From e8a1c7abd6e283e0a6133ab8e6612bd3fbfb7c9e Mon Sep 17 00:00:00 2001 From: Takashi Kokubun Date: Thu, 28 Jan 2016 13:22:10 +0900 Subject: [PATCH 2/2] Don't hardcode line_info line number --- lib/sassc/error.rb | 6 ++++-- test/error_test.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) 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