/
errors.ts
184 lines (162 loc) · 5.25 KB
/
errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* See the official documentation for more details:
*
* https://docs.microsoft.com/en-us/azure/active-directory/develop/v1-protocols-oauth-code#error-response-1
*
* NOTE: This documentation is for v1 OAuth support but the same error
* response details still apply to v2.
*/
export interface ErrorResponse {
/**
* The string identifier for the error.
*/
error: string;
/**
* The error's description.
*/
errorDescription: string;
/**
* An array of codes pertaining to the error(s) that occurred.
*/
errorCodes?: number[];
/**
* The timestamp at which the error occurred.
*/
timestamp?: string;
/**
* The trace identifier for this error occurrence.
*/
traceId?: string;
/**
* The correlation ID to be used for tracking the source of the error.
*/
correlationId?: string;
}
/**
* Used for internal deserialization of OAuth responses. Public model is ErrorResponse
* @internal
*/
export interface OAuthErrorResponse {
error: string;
error_description: string;
error_codes?: number[];
timestamp?: string;
trace_id?: string;
correlation_id?: string;
}
function isErrorResponse(errorResponse: any): errorResponse is OAuthErrorResponse {
return (
errorResponse &&
typeof errorResponse.error === "string" &&
typeof errorResponse.error_description === "string"
);
}
/**
* The Error.name value of an CredentialUnavailable
*/
export const CredentialUnavailableErrorName = "CredentialUnavailableError";
/**
* This signifies that the credential that was tried in a chained credential
* was not available to be used as the credential. Rather than treating this as
* an error that should halt the chain, it's caught and the chain continues
*/
export class CredentialUnavailableError extends Error {
constructor(message?: string) {
super(message);
this.name = CredentialUnavailableErrorName;
}
}
/**
* The Error.name value of an AuthenticationError
*/
export const AuthenticationErrorName = "AuthenticationError";
/**
* Provides details about a failure to authenticate with Azure Active
* Directory. The `errorResponse` field contains more details about
* the specific failure.
*/
export class AuthenticationError extends Error {
/**
* The HTTP status code returned from the authentication request.
*/
public readonly statusCode: number;
/**
* The error response details.
*/
public readonly errorResponse: ErrorResponse;
// eslint-disable-next-line @typescript-eslint/ban-types
constructor(statusCode: number, errorBody: object | string | undefined | null) {
let errorResponse: ErrorResponse = {
error: "unknown",
errorDescription: "An unknown error occurred and no additional details are available."
};
if (isErrorResponse(errorBody)) {
errorResponse = convertOAuthErrorResponseToErrorResponse(errorBody);
} else if (typeof errorBody === "string") {
try {
// Most error responses will contain JSON-formatted error details
// in the response body
const oauthErrorResponse: OAuthErrorResponse = JSON.parse(errorBody);
errorResponse = convertOAuthErrorResponseToErrorResponse(oauthErrorResponse);
} catch (e) {
if (statusCode === 400) {
errorResponse = {
error: "authority_not_found",
errorDescription: "The specified authority URL was not found."
};
} else {
errorResponse = {
error: "unknown_error",
errorDescription: `An unknown error has occurred. Response body:\n\n${errorBody}`
};
}
}
} else {
errorResponse = {
error: "unknown_error",
errorDescription: "An unknown error occurred and no additional details are available."
};
}
super(
`${errorResponse.error}(status code ${statusCode}).\nMore details:\n${errorResponse.errorDescription}`
);
this.statusCode = statusCode;
this.errorResponse = errorResponse;
// Ensure that this type reports the correct name
this.name = AuthenticationErrorName;
}
}
/**
* The Error.name value of an AggregateAuthenticationError
*/
export const AggregateAuthenticationErrorName = "AggregateAuthenticationError";
/**
* Provides an `errors` array containing {@link AuthenticationError} instance
* for authentication failures from credentials in a {@link ChainedTokenCredential}.
*/
export class AggregateAuthenticationError extends Error {
/**
* The array of error objects that were thrown while trying to authenticate
* with the credentials in a {@link ChainedTokenCredential}.
*/
public errors: any[];
constructor(errors: any[], errorMessage?: string) {
const errorDetail = errors.join("\n");
super(`${errorMessage}\n\n${errorDetail}`);
this.errors = errors;
// Ensure that this type reports the correct name
this.name = AggregateAuthenticationErrorName;
}
}
function convertOAuthErrorResponseToErrorResponse(errorBody: OAuthErrorResponse): ErrorResponse {
return {
error: errorBody.error,
errorDescription: errorBody.error_description,
correlationId: errorBody.correlation_id,
errorCodes: errorBody.error_codes,
timestamp: errorBody.timestamp,
traceId: errorBody.trace_id
};
}