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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (getAgent): add nullish check for uri #239

Merged
merged 2 commits into from Jun 30, 2021

Conversation

jsjoeio
Copy link
Contributor

@jsjoeio jsjoeio commented Jun 28, 2021

This adds a fix to requestToFetchOptions to throw an exception if uri is missing when calling from teenyRequest.

We should do this because we can't guarantee that the value is always a string
(due to type assertions) or code calling it without a valid string.

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #236 馃

@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Jun 28, 2021
@jsjoeio jsjoeio marked this pull request as ready for review June 28, 2021 22:05
@jsjoeio jsjoeio requested a review from a team as a code owner June 28, 2021 22:05
Copy link
Contributor

@JustinBeckwith JustinBeckwith left a comment

Choose a reason for hiding this comment

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

Greetings, and thanks for the PR! Can you share a little more on the motivations here? The getAgent method requires this uri parameter because we use it. What is the scenario where this would be null/undefined?

@jsjoeio
Copy link
Contributor Author

jsjoeio commented Jun 28, 2021

Hey @JustinBeckwith - thanks for the quick response. Not sure if you saw the original issue which talked about the motivation behind this change, but happy to explain again here.

It looks like there is a type assertion on uri before it gets passed to getAgent here.

As a result, getAgent wrongly assumes uri is a string and calls .startsWith() here causing a runtime error.

I noticed this happening in codecov-node here.

The only real fix needed is agents.ts#L40 but I changed the parameter type annotation from string -> string | undefined due to the type assertion. Happy to make changes/go a different approach if you have a better solution!

@JustinBeckwith
Copy link
Contributor

Got it - thanks for the details! The type assertion you linked to makes the assumption that one of uri or url are going to be set in the request options. The problem is ... I guess someone could make a request with neither!? If someone makes a call to request with no uri or url, what would you expect it to do? I am thinking the most ideal outcome would be we throw, because otherwise I have no idea how it would work. Or am I misreading something?

@jsjoeio
Copy link
Contributor Author

jsjoeio commented Jun 28, 2021

I guess someone could make a request with neither!?

Exactly! And I think that's what's happening in codecov-node, leading to this error.

I am thinking the most ideal outcome would be we throw, because otherwise I have no idea how it would work

Lol same. That makes sense to me.

In that case, should I check if uri is undefined and throw if it is?

@JustinBeckwith
Copy link
Contributor

Yeah I think throwing with a reasonable error here makes sense. It should be backward compatible, because it was going to throw, just with a weird error anyways. I would throw right after we try to snag the uri here:
https://github.com/googleapis/teeny-request/blob/master/src/index.ts#L120

Thanks for sticking with me here!

@jsjoeio jsjoeio marked this pull request as draft June 29, 2021 21:07
@jsjoeio jsjoeio marked this pull request as ready for review June 29, 2021 21:25
@jsjoeio
Copy link
Contributor Author

jsjoeio commented Jun 29, 2021

It should be backward compatible, because it was going to throw, just with a weird error anyways. I would throw right after we try to snag the uri here

Makes sense. Thanks for the suggestion!

I added two tests as well for this change. Let me know what you think!

Thanks for sticking with me here!

Thanks for being kind, patient and helpful!

@JustinBeckwith JustinBeckwith added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 30, 2021
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 30, 2021
@JustinBeckwith JustinBeckwith added the owlbot:run Add this label to trigger the Owlbot post processor. label Jun 30, 2021
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Jun 30, 2021
@JustinBeckwith JustinBeckwith merged commit 4d770e3 into googleapis:master Jun 30, 2021
@jsjoeio jsjoeio deleted the fix-getAgent-bug branch June 30, 2021 16:21
gcf-merge-on-green bot pushed a commit that referenced this pull request Jun 30, 2021
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.

PR to fix undefined error in getAgent?
3 participants