Skip to content

Commit

Permalink
Show sass file on the top of backtrace
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Kokubun committed Jan 15, 2016
1 parent f7ff6bc commit c084854
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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
# The backtrace of the error within Sass files.
attr_accessor :sass_backtrace

def backtrace
return nil if super.nil?
sass_backtrace + super
end

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
23 changes: 23 additions & 0 deletions test/error_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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

0 comments on commit c084854

Please sign in to comment.