Commit 272518e0 by Adeel

Importer: Allows importer to return error.

* Adds corresponding tests.

Issue URL: #651.
PR URL: #817.
parent 4ed6de22
......@@ -16,16 +16,36 @@ SassImportList CustomImporterBridge::post_process_return_value(Handle<Value> val
for (size_t i = 0; i < array->Length(); ++i) {
Local<Value> value = array->Get(static_cast<uint32_t>(i));
if (!value->IsObject())
if (!value->IsObject()) {
continue;
}
Local<Object> object = Local<Object>::Cast(value);
char* path = create_string(object->Get(NanNew<String>("file")));
char* contents = create_string(object->Get(NanNew<String>("contents")));
imports[i] = sass_make_import_entry(path, (!contents || contents[0] == '\0') ? 0 : strdup(contents), 0);
if (value->IsNativeError()) {
char* message = create_string(object->Get(NanNew<String>("message")));
imports[i] = sass_make_import_entry(0, 0, 0);
sass_import_set_error(imports[i], message, -1, -1);
}
else {
char* path = create_string(object->Get(NanNew<String>("file")));
char* contents = create_string(object->Get(NanNew<String>("contents")));
imports[i] = sass_make_import_entry(path, (!contents || contents[0] == '\0') ? 0 : strdup(contents), 0);
}
}
}
else if (returned_value->IsNativeError()) {
imports = sass_make_import_list(1);
Local<Object> object = Local<Object>::Cast(returned_value);
char* message = create_string(object->Get(NanNew<String>("message")));
imports[0] = sass_make_import_entry(0, 0, 0);
sass_import_set_error(imports[0], message, -1, -1);
}
else if (returned_value->IsObject()) {
imports = sass_make_import_list(1);
Local<Object> object = Local<Object>::Cast(returned_value);
......@@ -46,7 +66,7 @@ std::vector<Handle<Value>> CustomImporterBridge::pre_process_args(std::vector<vo
std::vector<Handle<Value>> out;
for (void* ptr : in) {
out.push_back(NanNew<String>((char const*) ptr));
out.push_back(NanNew<String>((char const*)ptr));
}
return out;
......
......@@ -387,6 +387,30 @@ describe('api', function() {
done();
});
});
it('should reflect user-defined error when returned as callback', function(done) {
sass.render({
data: src,
importer: function(url, prev, done) {
done(new Error('doesn\'t exist!'));
}
}, function(error) {
assert.equal(error.message, 'doesn\'t exist!');
done();
});
});
it('should reflect user-defined error with return', function(done) {
sass.render({
data: src,
importer: function() {
return new Error('doesn\'t exist!');
}
}, function(error) {
assert.equal(error.message, 'doesn\'t exist!');
done();
});
});
});
describe('.render(functions)', function() {
......@@ -1136,6 +1160,20 @@ describe('api', function() {
assert.equal(sync, true);
done();
});
it('should throw user-defined error', function(done) {
assert.throws(function() {
sass.renderSync({
data: src,
importer: function() {
return new Error('doesn\'t exist!');
}
});
}, /doesn\'t exist!/);
done();
});
});
describe('.render({stats: {}})', function() {
......@@ -1390,14 +1428,10 @@ describe('api', function() {
assert.throws(function() {
fs.renameSync(originalBin, renamedBin);
process.sass.getBinaryPath(true);
}, function(err) {
fs.renameSync(renamedBin, originalBin);
}, /`libsass` bindings not found. Try reinstalling `node-sass`?/);
if ((err instanceof Error) && /`libsass` bindings not found. Try reinstalling `node-sass`?/.test(err)) {
done();
return true;
}
});
fs.renameSync(renamedBin, originalBin);
done();
});
});
});
......@@ -410,7 +410,7 @@ describe('cli', function() {
});
});
it('should return error on for invalid importer file path', function(done) {
it('should return error for invalid importer file path', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('non/existing/path')
......@@ -421,6 +421,18 @@ describe('cli', function() {
done();
});
});
it('should reflect user-defined Error', function(done) {
var bin = spawn(cli, [
src, '--output', path.dirname(dest),
'--importer', fixture('extras/my_custom_importer_error.js')
]);
bin.stderr.once('data', function(code) {
assert.equal(JSON.parse(code).message, 'doesn\'t exist!');
done();
});
});
});
describe('functions', function() {
......
module.exports = function() {
return new Error('doesn\'t exist!');
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment