From bbc8dc9582b0c5e345dbdcec3fee28721b2ff6e8 Mon Sep 17 00:00:00 2001 From: nex3 Date: Fri, 2 Feb 2007 06:15:28 +0000 Subject: [PATCH] Nicer error formatting, correct line numbering on errors thrown. git-svn-id: svn://hamptoncatlin.com/haml/trunk@334 7063305b-7217-0410-af8c-cdc13e5119b9 --- lib/sass/engine.rb | 8 ++++---- lib/sass/error.rb | 2 +- lib/sass/plugin.rb | 29 ++++++++++++++++++++++++++--- test/sass/plugin_test.rb | 12 +----------- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lib/sass/engine.rb b/lib/sass/engine.rb index 577be0e9..5f386ffe 100644 --- a/lib/sass/engine.rb +++ b/lib/sass/engine.rb @@ -30,7 +30,7 @@ module Sass # #-- # - # TODO: Add current options to REFRENCE. + # TODO: Add current options to REFRENCE. Remember :filename! # # When adding options, remember to add information about them # to README! @@ -59,7 +59,7 @@ module Sass root.to_s rescue SyntaxError => err - err.add_backtrace_entry + err.add_backtrace_entry(@options[:filename]) raise err end end @@ -109,7 +109,7 @@ module Sass def build_tree(index) line, tabs = @lines[index] index += 1 - @line = index + 1 + @line = index node = parse_line(line) # Node is nil if it's non-outputting, like a constant assignment @@ -119,7 +119,7 @@ module Sass while has_children child, index = build_tree(index) - child.line = index + child.line = @line node << child if child has_children = has_children?(index, tabs) end diff --git a/lib/sass/error.rb b/lib/sass/error.rb index 8b81ff35..cc163273 100644 --- a/lib/sass/error.rb +++ b/lib/sass/error.rb @@ -22,7 +22,7 @@ module Sass # Adds a properly formatted entry to the exception's backtrace. # +filename+ should be the file in which the error occurred, # if applicable (defaults to "(sass)"). - def add_backtrace_entry(filename = nil) # :nodoc: + def add_backtrace_entry(filename) # :nodoc: @sass_filename = filename self.backtrace ||= [] self.backtrace.unshift "#{filename || '(sass)'}:#{@sass_line}" diff --git a/lib/sass/plugin.rb b/lib/sass/plugin.rb index a41f687e..820d178a 100644 --- a/lib/sass/plugin.rb +++ b/lib/sass/plugin.rb @@ -45,14 +45,37 @@ module Sass css = css_filename(name) File.delete(css) if File.exists?(css) - engine = Engine.new(File.read(template_filename(name)), @@options.dup) + filename = template_filename(name) + options = @@options.dup + options[:filename] = filename + engine = Engine.new(File.read(filename), options) begin result = engine.render rescue Exception => e if RAILS_ENV != "production" - result = "#{e}\n\nBacktrace:\n#{e.backtrace.join("\n")}\n" + e_string = "#{e.class}: #{e.message}" + + if e.is_a? Sass::SyntaxError + e_string << "\non line #{e.sass_line}" + + if e.sass_filename + e_string << " of #{e.sass_filename}" + + if File.exists?(e.sass_filename) + e_string << "\n\n" + + min = [e.sass_line - 5, 0].max + File.read(e.sass_filename).rstrip.split("\n")[ + min .. e.sass_line + 5 + ].each_with_index do |line, i| + e_string << "#{min + i + 1}: #{line}\n" + end + end + end + end + result = "/*\n#{e_string}\n\nBacktrace:\n#{e.backtrace.join("\n")}\n*/" else - raise e + result = "/* Internal stylesheet error */" end end diff --git a/test/sass/plugin_test.rb b/test/sass/plugin_test.rb index 71b4dae3..eb3b5ce3 100644 --- a/test/sass/plugin_test.rb +++ b/test/sass/plugin_test.rb @@ -37,19 +37,9 @@ class SassPluginTest < Test::Unit::TestCase def test_exception_handling File.open(tempfile_loc('bork')) do |file| - assert_equal("Undefined constant: \"!bork\"", file.gets.strip) + assert_equal("/*\nSass::SyntaxError: Undefined constant: \"!bork\"\non line 2 of #{File.dirname(__FILE__) + '/templates/bork.sass'}\n\n1: bork\n2: :bork= !bork", file.read.split("\n")[0...6].join("\n")) end File.delete(tempfile_loc('bork')) - Sass.const_set('RAILS_ENV', 'production') - raised = false - begin - Sass::Plugin.update_stylesheets - rescue - raised = true - end - assert raised - assert !File.exists?(tempfile_loc('bork')) - Sass::Plugin.const_set('RAILS_ENV', 'testing') end def test_controller_process