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

Bug with destructuring args in a try/catch in an async function #5462

Closed
mattkrick opened this issue Mar 13, 2017 · 5 comments
Closed

Bug with destructuring args in a try/catch in an async function #5462

mattkrick opened this issue Mar 13, 2017 · 5 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@mattkrick
Copy link
Contributor

mattkrick commented Mar 13, 2017

Input Code

 const tryCatchWrapper = (target) => async (...targetArgs) => {
    try {
      return target(...targetArgs);
    } catch (e) {
      throw errorObj({_error: e.message});
   }
 };

Babel Configuration (.babelrc, package.json, cli command)

babeljs.io standard config

Expected Behavior

"use strict";

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var tryCatchWrapper = function tryCatchWrapper(target) {
  return function () {
    var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
      for (var _len = arguments.length, targetArgs = Array(_len), _key = 0; _key < _len; _key++) {
        targetArgs[_key] = arguments[_key];
      }

      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;

              targetArgs;
              return _context.abrupt("return", target.apply(undefined, targetArgs));

            case 5:
              _context.prev = 5;
              _context.t0 = _context["catch"](0);
              throw errorObj({ _error: _context.t0.message });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, undefined, [[0, 5]]);
    }));

    return function () {
      return _ref.apply(this, arguments);
    };
  }();
};

Current Behavior

"use strict";

var _arguments = arguments;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var tryCatchWrapper = function tryCatchWrapper(target) {
  return function () {
    var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
      var _args = _arguments;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;
              return _context.abrupt("return", target.apply(undefined, _args));

            case 4:
              _context.prev = 4;
              _context.t0 = _context["catch"](0);
              throw errorObj({ _error: _context.t0.message });

            case 7:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, undefined, [[0, 4]]);
    }));

    return function () {
      return _ref.apply(this, arguments);
    };
  }();
};

Possible Solution

Reference the destructured args:

 const tryCatchWrapper = (target) => async (...targetArgs) => {
    try {
      targetArgs; // do this!
      return target(...targetArgs);
    } catch (e) {
      throw errorObj({_error: e.message});
   }
 };

Context

Made a try/catch wrapper for calling stripe. it bugs out! But then when i put a console.log in there, it works fine, which makes it extra confusing.
A call might look like this:

stripe.invoices.retrieveUpcoming = tryCatchWrapper(stripe.invoices.retrieveUpcoming.bind(stripe.invoices));

Your Environment

babeljs.io

@babel-bot
Copy link
Collaborator

Hey @mattkrick! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@montogeek
Copy link

What is

babeljs.io standard config

?

{
  "presets": ["env"]
}

?

@mattkrick
Copy link
Contributor Author

mattkrick commented Mar 14, 2017 via email

@existentialism
Copy link
Member

Dupe of #4270 #4219 #3077

@aaronang
Copy link
Member

Closing this issue because it is a duplicate. Thank you for taking the time to report though 😄

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 5, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

5 participants