Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cause): added error cause #32

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function isModule(file) {
// and try to load it as javascript. That won't work for this case.
var ext = path.extname(file);
// in order for this to work after deprecation of module.extensions
var extensions = {'.js': true, '.json': true, '.node': true}
var extensions = {'.js': true, '.json': true, '.node': true};
return ext === '' || extensions.hasOwnProperty(ext);
}

Expand All @@ -47,15 +47,19 @@ exports.create = function create(parent) {

fs.readFile(file, 'utf8', function (err, data) {
if (err) {
callback(err);
callback(new Error(`Error occured while reading file ${file}`, {
cause: err
}));
return;
}

try {
data = JSON.parse(data);
resolve(data, file, callback);
} catch (err) {
callback(err);
callback(new Error(`Error occured while parsing JSON data for file ${file}`, {
cause: err
}));
}
});
}
Expand Down
31 changes: 30 additions & 1 deletion lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ exports.create = function create(parent) {

// Remove protocol prefix
data = data.slice(handler.protocol.length + 1);
const proto = handler.protocol;

tasks = self.getStack(handler.protocol).map(function (handler) {
if (handler.length < 2) {
Expand All @@ -188,12 +189,40 @@ exports.create = function create(parent) {
try {
data = handler(input);
} catch (err) {
error = err;
error = new Error(`Error occurred while resolving "${proto}" protocol with value "${input}" at "${handler.name || 'unknown'}" handler`, {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After closely looking at this, I think right now, we are only dealing with synchronous handlers function handler(input), but we actually need to do something similar for asynchronous handlers, which have a different signature function handler(input, cb).

I think we'll need to do wrap L201 with a callback that simply wraps handler and decorates errors with the additional information before propagating. Something like:

- return handler
+return function handl(...args) {
+   const next = args[args.length - 1];
+
+   function cb(err, ...ar) {
+        return next(new Error('error with more info', { cause: err }), ...ar);
+   }
+
+    args[args.length - 1] = cb;
+
+    return handler(...args);
+}

The changes should look roughly like that, but I'd test and update/add new tests if necessary to ensure those changes behave as intended.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added more cases

cause: err,
});
}

done(error, data);
};
}
if (handler.length == 2) {
return function wrapper(input, done) {
function cb(err, ...args) {
if (err) {
const error = new Error(`Error occured while resolving "${proto}" protocol with value "${input}" at "${handler.name || 'unknown'}" handler`, {
cause: err,
});
return done(error, ...args);
}
return done(err, ...args);
}
return handler(input, cb);
}
}
return function wrapper(input, filename, done){
function cb(err, ...args) {
if (err) {
const error = new Error(`Error occured while resolving "${proto}" protocol with value "${input}" at "${handler.name || 'unknown'}" handler`, {
cause: err,
});
return done(error, ...args);
}
return done(err, ...args);
}
return handler(input, filename, cb);
}

return handler;
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shortstop",
"version": "1.0.2",
"version": "1.1.0-beta.1",
"description": "Enable use of protocols (such as file:, buffer:, or method:) in configuration files.",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ test('shortstop', function (t) {
expected = { foo: 'foo:foo', bar: false };
resolver.resolve(expected, function resolve(err, actual) {
t.ok(err);
t.equal(err.message, 'fail');
t.equal(err.message, `Error occured while resolving "foo" protocol with value "foo" at "err" handler`);
t.equal(err.cause.message, 'fail');
t.notOk(actual);
t.end();
});
Expand All @@ -247,7 +248,8 @@ test('shortstop', function (t) {
expected = { foo: 'test:foo', bar: false };
resolver.resolve(expected, function resolve(err, actual) {
t.ok(err);
t.equal(err.message, 'fail');
t.equal(err.message, `Error occurred while resolving "test" protocol with value "foo" at "err" handler`);
t.equal(err.cause.message, 'fail');
t.notOk(actual);
t.end();
});
Expand Down