Skip to content

Commit 41def27

Browse files
authored
Update error kind when context is incorrect (#14013)
1 parent 65b4288 commit 41def27

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/Accounts/Authentication/Factories/AuthenticationFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
2323
using Microsoft.Azure.Commands.Common.Authentication.Authentication;
2424
using Microsoft.Azure.Commands.Common.Authentication.Properties;
25+
using Microsoft.Azure.Commands.Common.Exceptions;
2526
using Microsoft.Identity.Client;
2627
using Microsoft.Rest;
2728

@@ -285,7 +286,7 @@ public ServiceClientCredentials GetServiceClientCredentials(IAzureContext contex
285286
{
286287
if (context.Account == null)
287288
{
288-
throw new ArgumentException(Resources.ArmAccountNotFound);
289+
throw new AzPSArgumentException(Resources.ArmAccountNotFound, "context.Account", ErrorKind.UserError);
289290
}
290291
switch (context.Account.Type)
291292
{

src/Accounts/Authentication/Factories/ClientFactory.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#endif
2020
using Microsoft.Azure.Commands.Common.Authentication.Models;
2121
using Microsoft.Azure.Commands.Common.Authentication.Properties;
22+
using Microsoft.Azure.Commands.Common.Exceptions;
2223
using System;
2324
using System.Collections.Concurrent;
2425
using System.Collections.Generic;
@@ -56,7 +57,7 @@ public virtual TClient CreateArmClient<TClient>(IAzureContext context, string en
5657
{
5758
if (context == null)
5859
{
59-
throw new ApplicationException(Resources.NoSubscriptionInContext);
60+
throw new AzPSApplicationException(Resources.NoSubscriptionInContext, ErrorKind.UserError);
6061
}
6162

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

135136
SubscriptionCloudCredentials creds = AzureSession.Instance.AuthenticationFactory.GetSubscriptionCloudCredentials(context, endpoint);
@@ -165,21 +166,21 @@ public virtual TClient CreateClient<TClient>(IAzureContextContainer profile, IAz
165166
{
166167
if (subscription == null)
167168
{
168-
throw new ApplicationException(Resources.InvalidDefaultSubscription);
169+
throw new AzPSApplicationException(Resources.InvalidDefaultSubscription, ErrorKind.UserError);
169170
}
170171

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

173174
if (null == account)
174175
{
175-
throw new ArgumentException(string.Format("Account with name '{0}' does not exist.", subscription.GetAccount()), "accountName");
176+
throw new AzPSArgumentException(string.Format("Account with name '{0}' does not exist.", subscription.GetAccount()), "accountName", ErrorKind.UserError);
176177
}
177178

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

180181
if (null == environment)
181182
{
182-
throw new ArgumentException(string.Format(Resources.EnvironmentNotFound, subscription.GetEnvironment()));
183+
throw new AzPSArgumentException(string.Format(Resources.EnvironmentNotFound, subscription.GetEnvironment()), "environment", ErrorKind.UserError);
183184
}
184185

185186
AzureContext context = new AzureContext(subscription, account, environment);

0 commit comments

Comments
 (0)