Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show sass file on the top of backtrace #34

Merged
merged 2 commits into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/sassc/error.rb
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
37 changes: 37 additions & 0 deletions test/error_test.rb
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