-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from k0kubun/sass_backtrace
Show sass file on the top of backtrace
- Loading branch information
Showing
2 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,30 @@ | ||
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 | ||
LINE_INFO_REGEX = /on line (\d+) of (.+)/ | ||
|
||
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").find { |line| line.match(LINE_INFO_REGEX) } | ||
return [] unless line_info | ||
|
||
_, line, filename = line_info.match(LINE_INFO_REGEX).to_a | ||
["#{Pathname.getwd.join(filename)}:#{line}"] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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 | ||
|
||
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 |