From ecb5eeae1ce52e4898a8c9a68f87962321c395f7 Mon Sep 17 00:00:00 2001 From: tdreyno Date: Sat, 8 Aug 2009 17:17:34 -0700 Subject: [PATCH] add sass exception handling --- lib/middleman.rb | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/middleman.rb b/lib/middleman.rb index 3d922f33..daacf9c2 100644 --- a/lib/middleman.rb +++ b/lib/middleman.rb @@ -83,7 +83,11 @@ class Middleman < Sinatra::Base renderer = "markaby" if renderer == "mab" result = if renderer == "sass" content_type 'text/css', :charset => 'utf-8' - sass(@template, Compass.sass_engine_options) + begin + sass(@template, Compass.sass_engine_options) + rescue Exception => e + sass_exception_string(e) + end else send(renderer.to_sym, @template) end @@ -93,6 +97,45 @@ class Middleman < Sinatra::Base result || pass end + + # Handle Sass errors + def sass_exception_string(e) + 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 + begin + 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 + rescue + e_string << "Couldn't read sass file: #{e.sass_filename}" + end + end + end + end + <