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

Error Handling Anti-Pattern #15

Closed
CodeMan99 opened this issue Feb 26, 2018 · 1 comment
Closed

Error Handling Anti-Pattern #15

CodeMan99 opened this issue Feb 26, 2018 · 1 comment
Assignees

Comments

@CodeMan99
Copy link
Contributor

I see that you are actively refactoring for various reasons. You should consider changing the error handling as well. In a few places you have something like the following.

try {
   if (err) {
      return callback(err);
   }
   // more stuff
} catch (e) {
   callback(e);
}

The usage of the callback inside of your try-catch is bad, especially in streams. That callback quite often synchronously calls the emit method. The emit method will also intentionally re-throw errors if there are no handlers. This is done because crashing is better than silently swallowing errors.

Alex Early explains this well: caolan/async#1458 (comment)

For most of your cases I suggest something like the following.

if (err) {
   return callback(new PluginError(PLUGIN_NAME, err));
}

var e, result;

try {
   result = processSomething();
} catch (err) {
   e = new PluginError(PLUGIN_NAME, err);
}

callback(e, result);
@crissdev crissdev self-assigned this Mar 7, 2018
@crissdev
Copy link
Owner

crissdev commented Mar 7, 2018

Thanks @CodeMan99

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

No branches or pull requests

2 participants