Skip to content

Commit 502925b

Browse files
authored
[Identity] Error name alignment (#14690)
This PR renames errors `CredentialUnavailable` to `CredentialUnavailableError`, and `AuthenticationRequired` to `AuthenticationRequiredError`, to improve the readability of these exported classes. I couldn't find any instance of any of our packages using these error names (other than within Identity itself). Fixes #14662
1 parent 7ca03aa commit 502925b

24 files changed

+71
-70
lines changed

sdk/identity/identity/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 2.0.0-beta.2 (Unreleased)
44

5+
- Breaking change: Renamed errors `CredentialUnavailable` to `CredentialUnavailableError`, and `AuthenticationRequired` to `AuthenticationRequiredError`, to align with the naming convention used for error classes in the Azure SDKs in JavaScript.
56
- Added `clientId` to the `AuthenticationRecord` type.
67
- `AuthenticationRecord` no longer has a `serialize` method. This method is now called `serializeAuthenticationRecord` and is a function exported at the top level of the Identity library, similar to `deserializeAuthenticationRecord`.
78
- `serializeAuthenticationRecord` now serializes into a JSON string with camel case properties. This makes it re-usable across languages.

sdk/identity/identity/review/identity.api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export interface AuthenticationRecord {
4141
}
4242

4343
// @public
44-
export class AuthenticationRequired extends CredentialUnavailable {
44+
export class AuthenticationRequiredError extends CredentialUnavailableError {
4545
constructor(
4646
scopes: string[],
4747
getTokenOptions?: GetTokenOptions, message?: string);
@@ -108,12 +108,12 @@ export interface ClientSecretCredentialOptions extends TokenCredentialOptions {
108108
}
109109

110110
// @public
111-
export class CredentialUnavailable extends Error {
111+
export class CredentialUnavailableError extends Error {
112112
constructor(message?: string);
113113
}
114114

115115
// @public
116-
export const CredentialUnavailableName = "CredentialUnavailable";
116+
export const CredentialUnavailableErrorName = "CredentialUnavailableError";
117117

118118
// @public
119119
export class DefaultAzureCredential extends ChainedTokenCredential {

sdk/identity/identity/src/client/errors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ function isErrorResponse(errorResponse: any): errorResponse is OAuthErrorRespons
6565
/**
6666
* The Error.name value of an CredentialUnavailable
6767
*/
68-
export const CredentialUnavailableName = "CredentialUnavailable";
68+
export const CredentialUnavailableErrorName = "CredentialUnavailableError";
6969

7070
/**
7171
* This signifies that the credential that was tried in a chained credential
7272
* was not available to be used as the credential. Rather than treating this as
7373
* an error that should halt the chain, it's caught and the chain continues
7474
*/
75-
export class CredentialUnavailable extends Error {
75+
export class CredentialUnavailableError extends Error {
7676
constructor(message?: string) {
7777
super(message);
78-
this.name = CredentialUnavailableName;
78+
this.name = CredentialUnavailableErrorName;
7979
}
8080
}
8181

sdk/identity/identity/src/credentials/authorizationCodeCredential.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import qs from "qs";
55
import { createSpan } from "../util/tracing";
6-
import { CredentialUnavailable } from "../client/errors";
6+
import { CredentialUnavailableError } from "../client/errors";
77
import { TokenCredential, GetTokenOptions, AccessToken } from "@azure/core-http";
88
import { IdentityClient, TokenResponse, TokenCredentialOptions } from "../client/identityClient";
99
import { SpanStatusCode } from "@azure/core-tracing";
@@ -188,7 +188,7 @@ export class AuthorizationCodeCredential implements TokenCredential {
188188
const token = tokenResponse && tokenResponse.accessToken;
189189

190190
if (!token) {
191-
throw new CredentialUnavailable("Failed to retrieve a valid token");
191+
throw new CredentialUnavailableError("Failed to retrieve a valid token");
192192
}
193193
return token;
194194
} catch (err) {

sdk/identity/identity/src/credentials/azureCliCredential.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { TokenCredential, GetTokenOptions, AccessToken } from "@azure/core-http";
55
import { createSpan } from "../util/tracing";
6-
import { CredentialUnavailable } from "../client/errors";
6+
import { CredentialUnavailableError } from "../client/errors";
77
import { SpanStatusCode } from "@azure/core-tracing";
88
import { credentialLogger, formatSuccess, formatError } from "../util/logging";
99
import * as child_process from "child_process";
@@ -91,19 +91,19 @@ export class AzureCliCredential implements TokenCredential {
9191
obj.stderr.match("az:(.*)not found") ||
9292
obj.stderr.startsWith("'az' is not recognized");
9393
if (isNotInstallError) {
94-
const error = new CredentialUnavailable(
94+
const error = new CredentialUnavailableError(
9595
"Azure CLI could not be found. Please visit https://aka.ms/azure-cli for installation instructions and then, once installed, authenticate to your Azure account using 'az login'."
9696
);
9797
logger.getToken.info(formatError(scopes, error));
9898
throw error;
9999
} else if (isLoginError) {
100-
const error = new CredentialUnavailable(
100+
const error = new CredentialUnavailableError(
101101
"Please run 'az login' from a command prompt to authenticate before using this credential."
102102
);
103103
logger.getToken.info(formatError(scopes, error));
104104
throw error;
105105
}
106-
const error = new CredentialUnavailable(obj.stderr);
106+
const error = new CredentialUnavailableError(obj.stderr);
107107
logger.getToken.info(formatError(scopes, error));
108108
throw error;
109109
} else {

sdk/identity/identity/src/credentials/chainedTokenCredential.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT license.
33

44
import { AccessToken, TokenCredential, GetTokenOptions } from "@azure/core-http";
5-
import { AggregateAuthenticationError, CredentialUnavailable } from "../client/errors";
5+
import { AggregateAuthenticationError, CredentialUnavailableError } from "../client/errors";
66
import { createSpan } from "../util/tracing";
77
import { SpanStatusCode } from "@azure/core-tracing";
88
import { credentialLogger, formatSuccess, formatError } from "../util/logging";
@@ -61,7 +61,7 @@ export class ChainedTokenCredential implements TokenCredential {
6161
try {
6262
token = await this._sources[i].getToken(scopes, updatedOptions);
6363
} catch (err) {
64-
if (err.name === "CredentialUnavailable") {
64+
if (err.name === "CredentialUnavailableError") {
6565
errors.push(err);
6666
} else {
6767
logger.getToken.info(formatError(scopes, err));
@@ -85,7 +85,7 @@ export class ChainedTokenCredential implements TokenCredential {
8585
logger.getToken.info(formatSuccess(scopes));
8686

8787
if (token === null) {
88-
throw new CredentialUnavailable("Failed to retrieve a valid token");
88+
throw new CredentialUnavailableError("Failed to retrieve a valid token");
8989
}
9090
return token;
9191
}

sdk/identity/identity/src/credentials/environmentCredential.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AccessToken, TokenCredential, GetTokenOptions } from "@azure/core-http"
55
import { credentialLogger, processEnvVars, formatSuccess, formatError } from "../util/logging";
66
import { TokenCredentialOptions } from "../client/identityClient";
77
import { ClientSecretCredential } from "./clientSecretCredential";
8-
import { AuthenticationError, CredentialUnavailable } from "../client/errors";
8+
import { AuthenticationError, CredentialUnavailableError } from "../client/errors";
99
import { checkTenantId } from "../util/checkTenantId";
1010
import { trace } from "../util/tracing";
1111
import { ClientCertificateCredential } from "./clientCertificateCredential";
@@ -131,7 +131,7 @@ export class EnvironmentCredential implements TokenCredential {
131131
throw authenticationError;
132132
}
133133
}
134-
throw new CredentialUnavailable(
134+
throw new CredentialUnavailableError(
135135
"EnvironmentCredential is unavailable. No underlying credential could be used."
136136
);
137137
});

sdk/identity/identity/src/credentials/managedIdentityCredential/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { AccessToken, GetTokenOptions, TokenCredential } from "@azure/core-http";
55
import { IdentityClient, TokenCredentialOptions } from "../../client/identityClient";
66
import { createSpan } from "../../util/tracing";
7-
import { AuthenticationError, CredentialUnavailable } from "../../client/errors";
7+
import { AuthenticationError, CredentialUnavailableError } from "../../client/errors";
88
import { SpanStatusCode } from "@azure/core-tracing";
99
import { credentialLogger, formatSuccess, formatError } from "../../util/logging";
1010
import { mapScopesToResource } from "./utils";
@@ -84,7 +84,7 @@ export class ManagedIdentityCredential implements TokenCredential {
8484
}
8585
}
8686

87-
throw new CredentialUnavailable("ManagedIdentityCredential - No MSI credential available");
87+
throw new CredentialUnavailableError("ManagedIdentityCredential - No MSI credential available");
8888
}
8989

9090
private async authenticateManagedIdentity(
@@ -147,7 +147,7 @@ export class ManagedIdentityCredential implements TokenCredential {
147147

148148
// It also means that the endpoint answered with either 200 or 201 (see the sendTokenRequest method),
149149
// yet we had no access token. For this reason, we'll throw once with a specific message:
150-
const error = new CredentialUnavailable(
150+
const error = new CredentialUnavailableError(
151151
"The managed identity endpoint was reached, yet no tokens were received."
152152
);
153153
logger.getToken.info(formatError(scopes, error));
@@ -161,7 +161,7 @@ export class ManagedIdentityCredential implements TokenCredential {
161161
} else {
162162
// We've previously determined that the endpoint was unavailable,
163163
// either because it was unreachable or permanently unable to authenticate.
164-
const error = new CredentialUnavailable(
164+
const error = new CredentialUnavailableError(
165165
"The managed identity endpoint is not currently available"
166166
);
167167
logger.getToken.info(formatError(scopes, error));
@@ -173,7 +173,7 @@ export class ManagedIdentityCredential implements TokenCredential {
173173
} catch (err) {
174174
// CredentialUnavailable errors are expected to reach here.
175175
// We intend them to bubble up, so that DefaultAzureCredential can catch them.
176-
if (err.name === "AuthenticationRequired") {
176+
if (err.name === "AuthenticationRequiredError") {
177177
throw err;
178178
}
179179

@@ -191,7 +191,7 @@ export class ManagedIdentityCredential implements TokenCredential {
191191
// If either the network is unreachable,
192192
// we can safely assume the credential is unavailable.
193193
if (err.code === "ENETUNREACH") {
194-
const error = new CredentialUnavailable(
194+
const error = new CredentialUnavailableError(
195195
"ManagedIdentityCredential is unavailable. Network unreachable."
196196
);
197197

@@ -202,7 +202,7 @@ export class ManagedIdentityCredential implements TokenCredential {
202202
// If either the host was unreachable,
203203
// we can safely assume the credential is unavailable.
204204
if (err.code === "EHOSTUNREACH") {
205-
const error = new CredentialUnavailable(
205+
const error = new CredentialUnavailableError(
206206
"ManagedIdentityCredential is unavailable. No managed identity endpoint found."
207207
);
208208

@@ -213,15 +213,15 @@ export class ManagedIdentityCredential implements TokenCredential {
213213
// If err.statusCode has a value of 400, it comes from sendTokenRequest,
214214
// and it means that the endpoint is working, but that no identity is available.
215215
if (err.statusCode === 400) {
216-
throw new CredentialUnavailable(
216+
throw new CredentialUnavailableError(
217217
"The managed identity endpoint is indicating there's no available identity"
218218
);
219219
}
220220

221221
// If the error has no status code, we can assume there was no available identity.
222222
// This will throw silently during any ChainedTokenCredential.
223223
if (err.statusCode === undefined) {
224-
throw new CredentialUnavailable(
224+
throw new CredentialUnavailableError(
225225
`ManagedIdentityCredential authentication failed. Message ${err.message}`
226226
);
227227
}

sdk/identity/identity/src/credentials/visualStudioCodeCredential.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ try {
1515
keytar = null;
1616
}
1717

18-
import { CredentialUnavailable } from "../client/errors";
18+
import { CredentialUnavailableError } from "../client/errors";
1919
import { credentialLogger, formatSuccess, formatError } from "../util/logging";
2020
import { AzureAuthorityHosts } from "../constants";
2121
import { checkTenantId } from "../util/checkTenantId";
@@ -34,7 +34,7 @@ function checkUnsupportedTenant(tenantId: string): void {
3434
// If the Tenant ID isn't supported, we throw.
3535
const unsupportedTenantError = unsupportedTenantIds[tenantId];
3636
if (unsupportedTenantError) {
37-
throw new CredentialUnavailable(unsupportedTenantError);
37+
throw new CredentialUnavailableError(unsupportedTenantError);
3838
}
3939
}
4040

@@ -172,7 +172,7 @@ export class VisualStudioCodeCredential implements TokenCredential {
172172
): Promise<AccessToken> {
173173
await this.prepareOnce();
174174
if (!keytar) {
175-
throw new CredentialUnavailable(
175+
throw new CredentialUnavailableError(
176176
"Visual Studio Code credential requires the optional dependency 'keytar' to work correctly"
177177
);
178178
}
@@ -222,14 +222,14 @@ export class VisualStudioCodeCredential implements TokenCredential {
222222
logger.getToken.info(formatSuccess(scopes));
223223
return tokenResponse.accessToken;
224224
} else {
225-
const error = new CredentialUnavailable(
225+
const error = new CredentialUnavailableError(
226226
"Could not retrieve the token associated with Visual Studio Code. Have you connected using the 'Azure Account' extension recently?"
227227
);
228228
logger.getToken.info(formatError(scopes, error));
229229
throw error;
230230
}
231231
} else {
232-
const error = new CredentialUnavailable(
232+
const error = new CredentialUnavailableError(
233233
"Could not retrieve the token associated with Visual Studio Code. Did you connect using the 'Azure Account' extension?"
234234
);
235235
logger.getToken.info(formatError(scopes, error));

sdk/identity/identity/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { TokenCredential } from "@azure/core-http";
55
import { DefaultAzureCredential } from "./credentials/defaultAzureCredential";
66

77
export { AuthenticationRecord } from "./msal/types";
8-
export { AuthenticationRequired } from "./msal/errors";
8+
export { AuthenticationRequiredError } from "./msal/errors";
99
export { serializeAuthenticationRecord, deserializeAuthenticationRecord } from "./msal/utils";
1010
export { TokenCredentialOptions } from "./client/identityClient";
1111
export { InteractiveCredentialOptions } from "./credentials/interactiveCredentialOptions";
@@ -49,8 +49,8 @@ export {
4949
AggregateAuthenticationError,
5050
AuthenticationErrorName,
5151
AggregateAuthenticationErrorName,
52-
CredentialUnavailable,
53-
CredentialUnavailableName
52+
CredentialUnavailableError,
53+
CredentialUnavailableErrorName
5454
} from "./client/errors";
5555

5656
export { TokenCredential, GetTokenOptions, AccessToken } from "@azure/core-http";

0 commit comments

Comments
 (0)