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

Added try catch block added for azlogin #10556

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

RaviAkshintala
Copy link
Contributor

@RaviAkshintala RaviAkshintala commented Sep 5, 2024

This PR adds a try-catch block for the azlogin function. * If an account is already logged in, it prints a message "Already logged in...". * If the login fails, it prints an error message "Login failed...". This change will help to handle the login process more gracefully and provide better user feedback.

Related issue:

#10236

Check list

  • Related issue / work item is attached
  • Tests are written (if applicable)
  • Documentation is updated (if applicable)
  • Changes are tested and related VM images are successfully generated

@feliasson
Copy link

feliasson commented Sep 12, 2024

I just wanted to add here that the current approach here for this try/catch block will not work, I know this because I have tested it.

When not logged in you will get the following:

ERROR: Please run 'az login' to setup account.
ERROR: The subscription of '<subscription>' doesn't exist in cloud 'AzureCloud'.

It happens because the Powershell try can not catch the error from an external program, so it does not get into the catch block and thereafter tries to set the subscription after the catch block (hence the 2nd error).

In #10602 I have solved this using the following way:

try {
    az account show -o none 2>$null || Write-Error $_
    Write-Verbose "Already logged in..."
}

The 2>$null writes the error to $null if not logged in to silence it, the pipeline chain operator || runs the next command because LASTEXITCODE is not 0, so the Write-Error $_ is there just to cause a Powershell error to get into the catch block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants