Skip to content

Catch unhandled promise rejections - #196

Merged
daffl merged 3 commits into
feathersjs:masterfrom
betarabbit:master
Mar 30, 2019
Merged

daffl merged 3 commits into
feathersjs:masterfrom
betarabbit:master

Conversation

@betarabbit

Copy link
Copy Markdown
Contributor

Summary

(If you have not already please refer to the contributing guideline as described
here
)

  • Tell us about the problem your pull request is solving.

When use transaction hooks, if a transaction failed in either establishing or execution phase, an unhandled rejection error happens. The reason is that knex.transaction returns a promise and no error handler is attached to the promise within a turn of the event loop. Unhandled promise rejections are deprecated and it will terminate Node.js process with a non-zero exit code, better to catch them and throw a customized error instead.

Here is a simplified example for your reference:

// mock async function to throw an error
const asyncError = fn => {
  return Promise.resolve()
    .then(() => { throw new Error('error'); })
    .then(fn);
}

// hook list
const arr = [
  hook => {
    hook.params.test1 = 1;
    return hook;
  },
  hook => Promise.resolve(hook).then(hook => {
    hook.params.test2 = 2;
    return hook;
  }),
  // hook passes down error occurred in `asyncError`
  hook => new Promise((resolve, reject) => {
    asyncError(obj => resolve(obj)).catch(err => reject(err));
  })
  .then(obj => {
    hook.params.success = { obj };
    return hook;
  })
  .catch(err => {
    throw err;
  }),
  // hook neglects error occurred in `asyncError`,
  // comment the hook above and uncomment this one to see an unhandled rejection
  // hook => new Promise(resolve => {
  //   asyncError(obj => {
  //     hook.params.failure = { obj };
  //     return resolve(hook);
  //   })
  //   .catch(err => {
  //     console.log('catch 2');
  //     throw err;
  //   })
  // })
]

// simulate hook execution
arr.reduce((promise, fn) =>
  promise.then(hook => {
    console.log(hook);
    return hook === 'SKIP' ? 'SKIP' : fn(hook);
  }), Promise.resolve({ params: {} }))
  .then(result => console.log(result))
  .catch(err => {
    console.error(err);
  });
  • Are there any open issues that are related to this?
    No.
  • Is this PR dependent on PRs in other repos?
    No.

If so, please mention them to keep the conversations linked together.

Other Information

If there's anything else that's important and relevant to your pull
request, mention that information here. This could include
benchmarks, or other information.

Your PR will be reviewed by a core team member and they will work with you to get your changes merged in a timely manner. If merged your PR will automatically be added to the changelog in the next release.

If your changes involve documentation updates please mention that and link the appropriate PR in feathers-docs.

Thanks for contributing to Feathers! ❤️

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.

2 participants