Skip to content

Commit

Permalink
[Sass] When :full_exception is false, raise errors in Ruby.
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Dec 24, 2009
1 parent 09c3748 commit 64f1ada
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
4 changes: 4 additions & 0 deletions doc-src/SASS_CHANGELOG.md
Expand Up @@ -5,6 +5,10 @@

## [2.2.16](http://github.com/nex3/haml/commit/2.2.16)

* When the {file:SASS_REFERENCE.md#full_exception-option `:full_exception` option}
is false, raise the error in Ruby code rather than swallowing it
and printing something uninformative.

* Fixed error-reporting when something goes wrong when loading Sass
using the `sass` executable.
This used to raise a NameError because `Sass::SyntaxError` wasn't defined.
Expand Down
11 changes: 7 additions & 4 deletions doc-src/SASS_REFERENCE.md
Expand Up @@ -152,10 +152,13 @@ Available options are:

{#full_exception-option} `:full_exception`
: Whether an error in the Sass code
should cause Sass to provide a detailed description.
If set to true, the specific error will be displayed
along with a line number and source snippet.
Otherwise, a simple uninformative error message will be displayed.
should cause Sass to provide a detailed description
within the generated CSS file.
If set to true, the error will be displayed
along with a line number and source snippet
both as a comment in the CSS file
and at the top of the page (in supported browsers).
Otherwise, an exception will be raised in the Ruby code.
Defaults to false in production mode, true otherwise.
Only has meaning within Rack, Ruby on Rails, or Merb.

Expand Down
49 changes: 23 additions & 26 deletions lib/sass/plugin.rb
Expand Up @@ -95,6 +95,7 @@ def update_stylesheet(name, template_location, css_location)
result = begin
Sass::Files.tree_for(filename, engine_options(:css_filename => css, :filename => filename)).render
rescue Exception => e
raise e unless options[:full_exception]
exception_string(e)
end

Expand Down Expand Up @@ -136,32 +137,31 @@ def css_locations
end

def exception_string(e)
if options[:full_exception]
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}"
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
end
<<END
/*
#{e_string}
Expand All @@ -172,10 +172,7 @@ def exception_string(e)
font-family: monospace;
content: "#{e_string.gsub('"', '\"').gsub("\n", '\\A ')}"; }
END
# Fix an emacs syntax-highlighting hiccup: '
else
"/* Internal stylesheet error */"
end
# Fix an emacs syntax-highlighting hiccup: '
end

def template_filename(name, path)
Expand Down
9 changes: 4 additions & 5 deletions test/sass/plugin_test.rb
Expand Up @@ -61,14 +61,13 @@ def test_full_exception_handling
end

def test_nonfull_exception_handling
old_full_exception = Sass::Plugin.options[:full_exception]
Sass::Plugin.options[:full_exception] = false

File.delete(tempfile_loc('bork'))
Sass::Plugin.update_stylesheets
assert_equal("/* Internal stylesheet error */", File.read(tempfile_loc('bork')))
File.delete(tempfile_loc('bork'))

Sass::Plugin.options[:full_exception] = true
assert_raise(Sass::SyntaxError) {Sass::Plugin.update_stylesheets}
ensure
Sass::Plugin.options[:full_exception] = old_full_exception
end

def test_two_template_directories
Expand Down

0 comments on commit 64f1ada

Please sign in to comment.