Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit f8b95c5

Browse files
authored
Merge pull request #34 from k0kubun/sass_backtrace
Show sass file on the top of backtrace
2 parents 45ddb5c + e8a1c7a commit f8b95c5

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/sassc/error.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1+
require 'pathname'
12
require 'sass/error'
23

34
module SassC
45
class BaseError < StandardError; end
5-
class SyntaxError < BaseError; end
66
class NotRenderedError < BaseError; end
77
class InvalidStyleError < BaseError; end
88
class UnsupportedValue < BaseError; end
9+
10+
# When dealing with SyntaxErrors,
11+
# it's important to provide filename and line number information.
12+
# This will be used in various error reports to users, including backtraces;
13+
class SyntaxError < BaseError
14+
LINE_INFO_REGEX = /on line (\d+) of (.+)/
15+
16+
def backtrace
17+
return nil if super.nil?
18+
sass_backtrace + super
19+
end
20+
21+
# The backtrace of the error within Sass files.
22+
def sass_backtrace
23+
line_info = message.split("\n").find { |line| line.match(LINE_INFO_REGEX) }
24+
return [] unless line_info
25+
26+
_, line, filename = line_info.match(LINE_INFO_REGEX).to_a
27+
["#{Pathname.getwd.join(filename)}:#{line}"]
28+
end
29+
end
930
end

test/error_test.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require_relative "test_helper"
2+
3+
module SassC
4+
class ErrorTest < MiniTest::Test
5+
def test_first_backtrace_is_sass
6+
line = 2
7+
filename = "app/assets/stylesheets/application.scss"
8+
9+
begin
10+
raise SassC::SyntaxError.new(<<-ERROR)
11+
Error: property "padding" must be followed by a ':'
12+
on line #{line} of #{filename}
13+
>> padding top: 10px;
14+
--^
15+
ERROR
16+
rescue SassC::SyntaxError => err
17+
expected = "#{Pathname.getwd.join(filename)}:#{line}"
18+
assert_equal expected, err.backtrace.first
19+
end
20+
21+
begin
22+
raise SassC::SyntaxError.new(<<-ERROR)
23+
Error: no mixin named border-radius
24+
25+
Backtrace:
26+
\t#{filename}:#{line}
27+
on line #{line} of #{filename}
28+
>> @include border-radius(5px);
29+
-------------^
30+
ERROR
31+
rescue SassC::SyntaxError => err
32+
expected = "#{Pathname.getwd.join(filename)}:#{line}"
33+
assert_equal expected, err.backtrace.first
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)