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 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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', {
cause: err
}));
}
});
}
Expand Down
8 changes: 6 additions & 2 deletions lib/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ exports.create = function create(parent) {
}

async.parallel(tasks, function (err, data) {
err ? callback(err) : callback(null, data);
err ? callback(new Error('Error occured while resolving protocols in case data is of type object/array', {
cause: err,
})) : callback(null, data);
});

} else if (thing.isString(data)) {
Expand All @@ -188,7 +190,9 @@ exports.create = function create(parent) {
try {
data = handler(input);
} catch (err) {
error = err;
error = new Error('Error occured while resolving protocols in case data is of type string', {
Copy link

@jonathansamines jonathansamines Mar 5, 2025

Choose a reason for hiding this comment

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

IIRC, ultimately only this "string" keys actually result in meaningful work that can cause external failures. On a typical configuration resolved through shorstop, you'll have something like:

{
  "complex": {
    "key": "protocol-a:first-value",
    "other": [
       { "child": "protocol-b:second-value" }
    ]
  }
}

In the example above, ultimately only leaf nodes (e.g strings) are actually processed through shorstop handlers, and therefore yield external work. Other intermediate nodes are either only traversed (e.g objects or arrays) or ignored (e.g other primitives).

So this is probably the most meaningful piece of code we can instrument with an additional error in the causal chain. As it contains key information that could help us identify the underlying issue:

  • What "protocol" we were processing when the failure happened?
  • What "value" we were processing when the failure happened?
  • What "handler" we were processing when the failure happened? (A note here, handlers are not typically named, but we can always try to guess by using the function name and fallback to something like "unknown" in case of anonymous functions)

For the sample configuration above, I would expect errors to contain all of that information:

Error occurred while resolving "protocol-a" protocol with value "first-value" at "handlerNameOrUnknown" handler.
 ...
 cause ...

Without this information, this intermediate error in the causal chain is not really useful, and will only be additional noise in our logs.

Copy link
Author

Choose a reason for hiding this comment

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

@jonathansamines new information added.

cause: err,
});
}

done(error, data);
Expand Down
7 changes: 5 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 protocols in case data is of type object/array');
t.equal(err.cause.message, 'fail');
t.notOk(actual);
t.end();
});
Expand All @@ -247,7 +248,9 @@ 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 occured while resolving protocols in case data is of type object/array');
t.equal(err.cause.message, 'Error occured while resolving protocols in case data is of type string');
t.equal(err.cause.cause.message, 'fail');
t.notOk(actual);
t.end();
});
Expand Down