nodejs-spanner: Transaction.insert generates unhandled promise rejections

Environment details

  • OS: hmm… Google Cloud Platform?
  • Node.js version: 8.9.x
  • npm version: yarn 1.1
  • @google-cloud/spanner version: 0.8.1

Steps to reproduce

  1. Create a read-write transaction
  2. Use transaction.insert, but insert data which do not match the destination table’s schema
  3. Run transaction.commit()

This will generate an unhandled promise rejection, which does not come from commit().

My code (simplified) looks like this:

try {
   transaction.insert('tableName', {fakeData: 'rejected!'});
   await transaction.commit();
} catch(error){
  ...
}

According to the docs, insert does not seem to accept a callback. However, it looks like it accepts a callback parameter in the source code.

So I tried adding a catch after the insert:

transaction.insert('tableName', {fakeDate: 'rejected!'}).catch(...); // No unhandled promise rejection

This does seem to catch the error. However, the rest of the code is still executed synchronously. So I tried this:

try {
   await transaction.insert('tableName', {fakeData: 'rejected!'}); // Use await instead of catch
   await transaction.commit();
} catch(error){
  ...
}

The error is caught, but if there’s no error, await (or the associated promise) never resolves!

try {
   await transaction.insert('tableName', {validData: 'should be good!'}); // hangs forever
   await transaction.commit();
} catch(error){
  ...
}
// Unhandled promise rejection

It seems like:

  • insert() in fact accepts a callback
  • A resolve is missing somewhere if the insert is a success

Thanks for your help!

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 17 (17 by maintainers)

Most upvoted comments

Thanks. I think this explains why the error was being thrown by insert and not commit.

@WaldoJeffers that’s a good point, I would expect that to throw an error as well! I’m going to open a PR with a fix shortly.