Skip to content

Commit 87fe0f3

Browse files
committed
improve uncaught exception for getToken
1 parent 7b033ad commit 87fe0f3

File tree

1 file changed

+42
-39
lines changed

1 file changed

+42
-39
lines changed

src/common/atlas/apiClient.ts

Lines changed: 42 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ export class ApiClient {
8989
return !!(this.oauth2Client && this.accessToken);
9090
}
9191

92-
public async hasValidAccessToken(): Promise<boolean> {
93-
const accessToken = await this.getAccessToken();
94-
return accessToken !== undefined;
92+
public async validateAccessToken(): Promise<void> {
93+
await this.getAccessToken();
9594
}
9695

9796
public async getIpInfo(): Promise<{
@@ -119,48 +118,52 @@ export class ApiClient {
119118
}>;
120119
}
121120

122-
async sendEvents(events: TelemetryEvent<CommonProperties>[]): Promise<void> {
123-
const headers: Record<string, string> = {
124-
Accept: "application/json",
125-
"Content-Type": "application/json",
126-
"User-Agent": this.options.userAgent,
127-
};
128-
129-
const accessToken = await this.getAccessToken();
130-
if (accessToken) {
131-
const authUrl = new URL("api/private/v1.0/telemetry/events", this.options.baseUrl);
132-
headers["Authorization"] = `Bearer ${accessToken}`;
133-
134-
try {
135-
const response = await fetch(authUrl, {
136-
method: "POST",
137-
headers,
138-
body: JSON.stringify(events),
139-
});
140-
141-
if (response.ok) {
142-
return;
143-
}
144-
145-
// If anything other than 401, throw the error
146-
if (response.status !== 401) {
147-
throw await ApiClientError.fromResponse(response);
148-
}
121+
public async sendEvents(events: TelemetryEvent<CommonProperties>[]): Promise<void> {
122+
if (!this.hasCredentials()) {
123+
await this.sendUnauthEvents(events);
124+
return;
125+
}
149126

150-
// For 401, fall through to unauthenticated endpoint
151-
delete headers["Authorization"];
152-
} catch (error) {
153-
// If the error is not a 401, rethrow it
154-
if (!(error instanceof ApiClientError) || error.response.status !== 401) {
127+
try {
128+
await this.validateAccessToken();
129+
await this.sendAuthEvents(events);
130+
} catch (error) {
131+
if (error instanceof ApiClientError) {
132+
if (error.response.status !== 401) {
155133
throw error;
156134
}
157-
158-
// For 401 errors, fall through to unauthenticated endpoint
159-
delete headers["Authorization"];
160135
}
136+
// send unauth events if the token is not valid
137+
await this.sendUnauthEvents(events);
138+
return;
161139
}
140+
}
141+
142+
private async sendAuthEvents(events: TelemetryEvent<CommonProperties>[]): Promise<void> {
143+
const authUrl = new URL("api/private/v1.0/telemetry/events", this.options.baseUrl);
144+
const response = await fetch(authUrl, {
145+
method: "POST",
146+
headers: {
147+
Accept: "application/json",
148+
"Content-Type": "application/json",
149+
"User-Agent": this.options.userAgent,
150+
Authorization: `Bearer ${this.accessToken}`,
151+
},
152+
body: JSON.stringify(events),
153+
});
154+
155+
if (!response.ok) {
156+
throw await ApiClientError.fromResponse(response);
157+
}
158+
}
159+
160+
private async sendUnauthEvents(events: TelemetryEvent<CommonProperties>[]): Promise<void> {
161+
const headers: Record<string, string> = {
162+
Accept: "application/json",
163+
"Content-Type": "application/json",
164+
"User-Agent": this.options.userAgent,
165+
};
162166

163-
// Send to unauthenticated endpoint (either as fallback from 401 or direct if no token)
164167
const unauthUrl = new URL("api/private/unauth/telemetry/events", this.options.baseUrl);
165168
const response = await fetch(unauthUrl, {
166169
method: "POST",

0 commit comments

Comments
 (0)