-
Notifications
You must be signed in to change notification settings - Fork 274
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
getEndpointAuthorization
does not throw when authorization was not set and is not optional
#775
Comments
Any updates on this? It may be worth mentioning that corresponding claims (not set and not optional ⇒ throws) are made about |
Hello, @SimonAlling. |
Not really, I just expected the function to throw because the comment said it would. I've since moved to
I don't think such an option should be added. Updating the comment might be enough; however, I'm quite surprised that
I can neither confirm nor dispute that because I'm not very familiar with the in-the-box tasks, but I can easily imagine that being the case.
Indeed it would! I might add that TypeScript's powerful abilities aren't being leveraged in the library as of today: const url = tl.getEndpointUrl(serviceEndpointID, false)
const username = tl.getEndpointAuthorizationParameter(serviceEndpointID, "username", false)
const password = tl.getEndpointAuthorizationParameter(serviceEndpointID, "password", false)
// `url`, `username` and `password` all have type `string | undefined`, even though an error would already have been thrown if any of them were `undefined`.
if (url === undefined || username === undefined || password === undefined) {
// This entire `if` statement is only needed to make the type checker understand that it need not worry about `undefined`.
// We could have used type assertions (`… as string`), but that may cause nasty bugs down the road.
throw new Error("The impossible happened.")
}
// `url`, `username` and `password` now have type `string`. This can easily be fixed by adding a few overloads, for example: +export function getEndpointUrl(id: string, optional: false): string;
+export function getEndpointUrl(id: string, optional: boolean): string | undefined;
export function getEndpointUrl(id: string, optional: boolean): string | undefined {
var urlval = process.env['ENDPOINT_URL_' + id];
if (!optional && !urlval) {
throw new Error(loc('LIB_EndpointNotExist', id));
}
debug(id + '=' + urlval);
return urlval;
}
|
Hello, @SimonAlling. |
Environment
azure-pipelines-task-lib version: 3.1.4
Issue Description
Excerpt from the JSDoc comment about
getEndpointAuthorization
innode/task.ts
:I believe this is incorrect.
Expected behaviour
The function throws if the authorization was not set and is not optional.
Actual behaviour
The function does not throw.
Steps to reproduce
Output when run on my local machine:
Note that it also says
NONEXISTENT exists true
. In fact, it always does that, because the expression being logged isand
aval
has typestring | undefined
, so its value will probably never benull
.The text was updated successfully, but these errors were encountered: