Permalink
Browse files

Cleanup parse error exceptions

  • Loading branch information...
1 parent b7c3e3d commit 3b22ceefe4024ffc88f300627cfe4e462e2cd3c0 @josh josh committed May 21, 2011
Showing with 19 additions and 17 deletions.
  1. +18 −16 lib/less_js.rb
  2. +1 −1 test/test_less_js.rb
View
@@ -2,8 +2,7 @@
require 'less_js/source'
module LessJs
- EngineError = ExecJS::RuntimeError
- CompilationError = ExecJS::ProgramError
+ class ParseError < StandardError; end
module Source
def self.path
@@ -24,7 +23,17 @@ def self.version
end
def self.context
- @context ||= ExecJS.compile(contents)
+ @context ||= ExecJS.compile <<-EOS
+ #{contents}
+
+ function compile(data) {
+ var result;
+ new less.Parser().parse(data, function(error, tree) {
+ result = [error, tree.toCSS()];
+ });
+ return result;
+ }
+ EOS
end
end
@@ -36,20 +45,13 @@ def version
# Compile a script (String or IO) to CSS.
def compile(script, options = {})
script = script.read if script.respond_to?(:read)
+ error, data = Source.context.call('compile', script)
- code = <<-EOS
- (function(input) {
- var resp = "error";
- new(less.Parser)().parse(input, function(error, tree) {
- resp = [error, tree.toCSS()]
- });
- return resp;
- })
- EOS
-
- (error, response) = Source.context.call(code, script)
- raise CompilationError, error if error
- response
+ if error
+ raise ParseError, error['message']
+ else
+ data
+ end
end
end
end
View
@@ -15,7 +15,7 @@ def test_compile_with_io
end
def test_compilation_error
- assert_raise LessJs::CompilationError do
+ assert_raise LessJs::ParseError do
LessJs.compile("&&&&.a")
end
end

0 comments on commit 3b22cee

Please sign in to comment.