{Core} Remove the try-catch logic of CloudError in get_default_location_from_resource_group()#20739
Conversation
| try: | ||
| rg = resource_client.resource_groups.get(namespace.resource_group_name) | ||
| except CloudError as ex: | ||
| except HttpResponseError as ex: |
There was a problem hiding this comment.
Perhaps we don't use try catch here to let azure.cli.core.parser.AzCliCommandParser.validation_error handle exceptions, such as azure.core.exceptions.ResourceNotFoundError?
And it works pretty well, just like what we are having right now.
def get_default_location_from_resource_group(cmd, namespace):
if not namespace.location:
from azure.cli.core.commands.client_factory import get_mgmt_service_client
resource_client = get_mgmt_service_client(cmd.cli_ctx, ResourceType.MGMT_RESOURCE_RESOURCES)
# We don't use try catch here to let azure.cli.core.parser.AzCliCommandParser.validation_error
# handle exceptions, such as azure.core.exceptions.ResourceNotFoundError
rg = resource_client.resource_groups.get(namespace.resource_group_name)
namespace.location = rg.location # pylint: disable=no-member
logger.debug("using location '%s' from resource group '%s'", namespace.location, rg.name)But the side effect is that the exit code would be 2:
If the ResourceNotFoundError is caught, the exit code would be 1:
There was a problem hiding this comment.
OK, makes sense~
Here raise the original specific error seems to be a better choice.
There was a problem hiding this comment.
As we discussed offline, we first maintain the latest exit code and error throwing ways, because it seems to work well at present. So I remove the try-catch logic now.
If additional information is needed to help locate related problem in the future, we can consider adding error log
CloudError for resource Track 2 SDKtry-catch logic of CloudError in get_default_location_from_resource_group
try-catch logic of CloudError in get_default_location_from_resource_grouptry-catch logic of CloudError in get_default_location_from_resource_group()
Co-authored-by: Jiashuo Li <4003950+jiasli@users.noreply.github.com>
Fix: #20732
Since the resource module has migrated to Track 2 #17783,
CloudErrorshould be replaced byHttpResponseErrorin Track 2, so submit this PR to modify itDescription
Testing Guide
History Notes
[Component Name 1] BREAKING CHANGE: az command a: Make some customer-facing breaking change.
[Component Name 2] az command b: Add some customer-facing feature.
This checklist is used to make sure that common guidelines for a pull request are followed.
The PR title and description has followed the guideline in Submitting Pull Requests.
I adhere to the Command Guidelines.
I adhere to the Error Handling Guidelines.