Skip to content

Commit

Permalink
Support error message with backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Kokubun committed Jan 28, 2016
1 parent cc0a7b9 commit bca406f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/sassc/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ 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
end

# 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
Expand Down
15 changes: 15 additions & 0 deletions test/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bca406f

Please sign in to comment.