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

fix: destroy ResourceStream with pre-flight error #236

Merged

Conversation

stephenplusplus
Copy link
Contributor

@stephenplusplus stephenplusplus commented Jul 28, 2020

RE: googleapis/nodejs-bigquery#707

(It's easier to view this PR with whitespace changes hidden: https://github.com/googleapis/nodejs-paginator/pull/236/files?diff=unified&w=1)

In many situations across our APIs, we validate the input from a user to a given method. So, if a user is assembling a query to send off to the API, sometimes we'll figure out that it won't work, and we'll throw before we even attempt the request. For example:

bigQuery.query = (input) => {
  if (input !== validInput) {
    throw Error('Invalid!')
  }
}
try {
  bigQuery.query({ ... invalid input ... });
} catch (e) {
  // e.message === "Invalid!"
}

Throwing and streams aren't very compatible due to the sync-vs-async nature, so this doesn't work:

bigQuery.createQueryStream = (input) => {
  const userStream = new Stream()

  // ResourceStream doesn't call request methods
  // until the user is actually using the stream.
  //
  // This mocks that scenario to demonstrate that we
  // are now off of the sync flow
  userStream.on('read', () => {
    if (input !== validInput) {
      throw Error('Invalid!')
    }
  })

  return userStream
}
try {
  bigQuery.createQueryStream({ ... invalid input ... })
    .on('error', () => {})
    .pipe(...);
} catch (e) {
  // Never fired
}

// Uncaught error is triggered

This change will wrap the calls to the request stream in a try/catch, and then destroy the stream with any error that was caught.

bigQuery.createQueryStream({ ... invalid input ... })
  .on('error', (err) => {
    // err.message === 'Invalid!'
  })
  .pipe(...);

@stephenplusplus stephenplusplus requested a review from a team as a code owner July 28, 2020 21:09
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Jul 28, 2020
@codecov
Copy link

codecov bot commented Jul 28, 2020

Codecov Report

Merging #236 into master will increase coverage by 0.09%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #236      +/-   ##
==========================================
+ Coverage   94.47%   94.56%   +0.09%     
==========================================
  Files           4        4              
  Lines         416      423       +7     
  Branches       45       46       +1     
==========================================
+ Hits          393      400       +7     
  Misses         23       23              
Impacted Files Coverage Δ
src/resource-stream.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 98bb9e0...d33c9f9. Read the comment docs.

}
);
} catch (e) {
this.destroy(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know streams very well. Should we be emitting an error event or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will do the same, it is basically:

this.emit('error', error);
this.end();

@stephenplusplus stephenplusplus merged commit d57beb4 into googleapis:master Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants