Skip to content

Commit

Permalink
Update error kind when context is incorrect (#14013)
Browse files Browse the repository at this point in the history
  • Loading branch information
dingmeng-xue authored Jan 26, 2021
1 parent 65b4288 commit 41def27
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.Common.Authentication.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Properties;
using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Identity.Client;
using Microsoft.Rest;

Expand Down Expand Up @@ -285,7 +286,7 @@ public ServiceClientCredentials GetServiceClientCredentials(IAzureContext contex
{
if (context.Account == null)
{
throw new ArgumentException(Resources.ArmAccountNotFound);
throw new AzPSArgumentException(Resources.ArmAccountNotFound, "context.Account", ErrorKind.UserError);
}
switch (context.Account.Type)
{
Expand Down
11 changes: 6 additions & 5 deletions src/Accounts/Authentication/Factories/ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#endif
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Commands.Common.Authentication.Properties;
using Microsoft.Azure.Commands.Common.Exceptions;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -56,7 +57,7 @@ public virtual TClient CreateArmClient<TClient>(IAzureContext context, string en
{
if (context == null)
{
throw new ApplicationException(Resources.NoSubscriptionInContext);
throw new AzPSApplicationException(Resources.NoSubscriptionInContext, ErrorKind.UserError);
}

var creds = AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, endpoint);
Expand Down Expand Up @@ -129,7 +130,7 @@ public virtual TClient CreateClient<TClient>(IAzureContext context, string endpo
var exceptionMessage = endpoint == AzureEnvironment.Endpoint.ServiceManagement
? Resources.InvalidDefaultSubscription
: Resources.NoSubscriptionInContext;
throw new ApplicationException(exceptionMessage);
throw new AzPSApplicationException(exceptionMessage, ErrorKind.UserError);
}

SubscriptionCloudCredentials creds = AzureSession.Instance.AuthenticationFactory.GetSubscriptionCloudCredentials(context, endpoint);
Expand Down Expand Up @@ -165,21 +166,21 @@ public virtual TClient CreateClient<TClient>(IAzureContextContainer profile, IAz
{
if (subscription == null)
{
throw new ApplicationException(Resources.InvalidDefaultSubscription);
throw new AzPSApplicationException(Resources.InvalidDefaultSubscription, ErrorKind.UserError);
}

var account = profile.Accounts.FirstOrDefault((a) => string.Equals(a.Id, (subscription.GetAccount()), StringComparison.OrdinalIgnoreCase));

if (null == account)
{
throw new ArgumentException(string.Format("Account with name '{0}' does not exist.", subscription.GetAccount()), "accountName");
throw new AzPSArgumentException(string.Format("Account with name '{0}' does not exist.", subscription.GetAccount()), "accountName", ErrorKind.UserError);
}

var environment = profile.Environments.FirstOrDefault((e) => string.Equals(e.Name, subscription.GetEnvironment(), StringComparison.OrdinalIgnoreCase));

if (null == environment)
{
throw new ArgumentException(string.Format(Resources.EnvironmentNotFound, subscription.GetEnvironment()));
throw new AzPSArgumentException(string.Format(Resources.EnvironmentNotFound, subscription.GetEnvironment()), "environment", ErrorKind.UserError);
}

AzureContext context = new AzureContext(subscription, account, environment);
Expand Down

0 comments on commit 41def27

Please sign in to comment.