Skip to content

Commit eb468fa

Browse files
committed
Sync cloud integration on Authorizatin problems
1 parent 889aa37 commit eb468fa

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/plus/integrations/integration.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,22 @@ export abstract class IntegrationBase<
269269
void this.ensureSession({ createIfNeeded: false });
270270
}
271271

272+
private static readonly requestExceptionLimit = 5;
273+
private static readonly syncDueToRequestExceptionLimit = 1;
274+
private syncCountDueToRequestException = 0;
272275
private requestExceptionCount = 0;
273276

274277
resetRequestExceptionCount(): void {
275278
this.requestExceptionCount = 0;
279+
this.syncCountDueToRequestException = 0;
280+
}
281+
282+
/**
283+
* Resets request exceptions without resetting the amount of syncs
284+
*/
285+
smoothifyRequestExceptionCount(): void {
286+
// On resync we reset exception count only to avoid infinitive syncs on failure
287+
this.requestExceptionCount = 0;
276288
}
277289

278290
async reset(): Promise<void> {
@@ -321,7 +333,17 @@ export abstract class IntegrationBase<
321333

322334
Logger.error(ex, scope);
323335

324-
if (ex instanceof AuthenticationError || ex instanceof RequestClientError) {
336+
if (ex instanceof AuthenticationError && this._session?.cloud) {
337+
if (this.syncCountDueToRequestException < IntegrationBase.syncDueToRequestExceptionLimit) {
338+
this.syncCountDueToRequestException++;
339+
this._session = {
340+
...this._session,
341+
expiresAt: new Date(Date.now() - 1),
342+
};
343+
} else {
344+
this.trackRequestException();
345+
}
346+
} else if (ex instanceof AuthenticationError || ex instanceof RequestClientError) {
325347
this.trackRequestException();
326348
}
327349
return defaultValue;
@@ -343,7 +365,7 @@ export abstract class IntegrationBase<
343365
trackRequestException(): void {
344366
this.requestExceptionCount++;
345367

346-
if (this.requestExceptionCount >= 5 && this._session !== null) {
368+
if (this.requestExceptionCount >= IntegrationBase.requestExceptionLimit && this._session !== null) {
347369
void showIntegrationDisconnectedTooManyFailedRequestsWarningMessage(this.name);
348370
void this.disconnect({ currentSessionOnly: true });
349371
}
@@ -409,7 +431,7 @@ export abstract class IntegrationBase<
409431
}
410432

411433
this._session = session ?? null;
412-
this.resetRequestExceptionCount();
434+
this.smoothifyRequestExceptionCount();
413435

414436
if (session != null) {
415437
await this.container.storage.storeWorkspace(this.connectedKey, true);
@@ -1385,8 +1407,7 @@ export abstract class HostingIntegration<
13851407
);
13861408
return { value: pullRequests, duration: Date.now() - start };
13871409
} catch (ex) {
1388-
Logger.error(ex, scope);
1389-
return { error: ex, duration: Date.now() - start };
1410+
return this.handleProviderException(ex, scope, { error: ex, duration: Date.now() - start });
13901411
}
13911412
}
13921413

0 commit comments

Comments
 (0)