Skip to content

Sync cloud integration on Authorization problems #4324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/plus/integrations/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,22 @@ export abstract class IntegrationBase<
void this.ensureSession({ createIfNeeded: false });
}

private static readonly requestExceptionLimit = 5;
private static readonly syncDueToRequestExceptionLimit = 1;
private syncCountDueToRequestException = 0;
private requestExceptionCount = 0;

resetRequestExceptionCount(): void {
this.requestExceptionCount = 0;
this.syncCountDueToRequestException = 0;
}

/**
* Resets request exceptions without resetting the amount of syncs
*/
smoothifyRequestExceptionCount(): void {
// On resync we reset exception count only to avoid infinitive syncs on failure
this.requestExceptionCount = 0;
}

async reset(): Promise<void> {
Expand Down Expand Up @@ -332,7 +344,17 @@ export abstract class IntegrationBase<

Logger.error(ex, scope);

if (ex instanceof AuthenticationError || ex instanceof RequestClientError) {
if (ex instanceof AuthenticationError && this._session?.cloud) {
if (this.syncCountDueToRequestException < IntegrationBase.syncDueToRequestExceptionLimit) {
this.syncCountDueToRequestException++;
this._session = {
...this._session,
expiresAt: new Date(Date.now() - 1),
};
} else {
this.trackRequestException();
}
} else if (ex instanceof AuthenticationError || ex instanceof RequestClientError) {
this.trackRequestException();
}
return defaultValue;
Expand Down Expand Up @@ -366,7 +388,7 @@ export abstract class IntegrationBase<
trackRequestException(): void {
this.requestExceptionCount++;

if (this.requestExceptionCount >= 5 && this._session !== null) {
if (this.requestExceptionCount >= IntegrationBase.requestExceptionLimit && this._session !== null) {
void showIntegrationDisconnectedTooManyFailedRequestsWarningMessage(this.name);
void this.disconnect({ currentSessionOnly: true });
}
Expand Down Expand Up @@ -432,7 +454,7 @@ export abstract class IntegrationBase<
}

this._session = session ?? null;
this.resetRequestExceptionCount();
this.smoothifyRequestExceptionCount();

if (session != null) {
await this.container.storage.storeWorkspace(this.connectedKey, true);
Expand Down Expand Up @@ -1408,8 +1430,7 @@ export abstract class HostingIntegration<
);
return { value: pullRequests, duration: Date.now() - start };
} catch (ex) {
Logger.error(ex, scope);
return { error: ex, duration: Date.now() - start };
return this.handleProviderException(ex, scope, { error: ex, duration: Date.now() - start });
}
}

Expand Down