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

Unexpected behavior when callback throws an error #1159

Closed
erickoledadevrel opened this issue May 10, 2018 · 2 comments · Fixed by #1161
Closed

Unexpected behavior when callback throws an error #1159

erickoledadevrel opened this issue May 10, 2018 · 2 comments · Fixed by #1161
Assignees
Labels
type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@erickoledadevrel
Copy link
Contributor

Consider the following small modification of the UrlShortener example in the README:

urlshortener.url.get(params, (err, res) => {
  if (err) return console.error('The API returned an error: ' + err);
  console.log(`Long url is ${res.data.longUrl}`);
  throw new Error('This error is not from the API');
});

Expected behavior

The final error is bubbled up the stack and logged in the console.

Actual behavior

The final error is funneled back into my callback and treated as an API error:

Long url is https://www.google.com/
The API returned an error: Error: This error is not from the API

This seems to be a side effect of setting the callback as the catch() handler for the promise. The catch executes not only for errors resulting from the API call but also for any errors thrown in the then() handler for a successful call.

I'm not sure what Promise interface Axios supports, but I think the solution would be to set the callback as the error handler for a specific then(), instead of as a catch():

createAPIRequestAsync<T>(parameters)
        .then(r => callback(null, r),
              e => callback(e));
@JustinBeckwith JustinBeckwith self-assigned this May 10, 2018
@JustinBeckwith JustinBeckwith added the type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. label May 10, 2018
@JustinBeckwith
Copy link
Contributor

Thanks for calling this out! Also double thanks for including the fix :) I submitted a PR over in #1161, so hopefully this clears things up!

@erickoledadevrel
Copy link
Contributor Author

No problem, thanks for the quick PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants