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

Conversation

dreamsparkx
Copy link

@dreamsparkx dreamsparkx commented Mar 5, 2025

lib/resolver.js Outdated
@@ -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.

@@ -188,7 +189,9 @@ 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants