Skip to content

Commit

Permalink
Adds better error handling to remote providers
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Nov 28, 2020
1 parent 1d97e38 commit ac9bfa4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 32 deletions.
50 changes: 29 additions & 21 deletions src/git/remotes/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ export class AuthenticationError extends Error {
}
}

export class ClientError extends Error {
constructor(private original: Error) {
super(original.message);

Error.captureStackTrace(this, ClientError);
}
}

// TODO@eamodio revisit how once authenticated, all remotes are always connected, even after a restart

export abstract class RemoteProviderWithApi extends RemoteProvider {
Expand All @@ -249,7 +257,7 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
return this._onDidChange.event;
}

private invalidAuthenticationCount = 0;
private invalidClientExceptionCount = 0;

constructor(domain: string, path: string, protocol?: string, name?: string, custom?: boolean) {
super(domain, path, protocol, name, custom);
Expand Down Expand Up @@ -314,7 +322,7 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
disconnect(silent: boolean = false): void {
const disconnected = this._session != null;

this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
this._prsByCommit.clear();
this._session = null;

Expand Down Expand Up @@ -351,13 +359,13 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {

try {
const author = await this.getProviderAccountForCommit(this._session!, ref, options);
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
return author;
} catch (ex) {
Logger.error(ex, cc);

if (ex instanceof AuthenticationError) {
this.handleAuthenticationException();
if (ex instanceof ClientError || ex instanceof AuthenticationError) {
this.handleClientException();
}
return undefined;
}
Expand Down Expand Up @@ -386,13 +394,13 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {

try {
const author = await this.getProviderAccountForEmail(this._session!, email, options);
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
return author;
} catch (ex) {
Logger.error(ex, cc);

if (ex instanceof AuthenticationError) {
this.handleAuthenticationException();
if (ex instanceof ClientError || ex instanceof AuthenticationError) {
this.handleClientException();
}
return undefined;
}
Expand All @@ -416,13 +424,13 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {

try {
const issueOrPullRequest = await this.getProviderIssueOrPullRequest(this._session!, id);
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
return issueOrPullRequest;
} catch (ex) {
Logger.error(ex, cc);

if (ex instanceof AuthenticationError) {
this.handleAuthenticationException();
if (ex instanceof ClientError || ex instanceof AuthenticationError) {
this.handleClientException();
}
return undefined;
}
Expand All @@ -449,13 +457,13 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {

try {
const pr = await this.getProviderPullRequestForBranch(this._session!, branch, options);
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
return pr;
} catch (ex) {
Logger.error(ex, cc);

if (ex instanceof AuthenticationError) {
this.handleAuthenticationException();
if (ex instanceof ClientError || ex instanceof AuthenticationError) {
this.handleClientException();
}
return undefined;
}
Expand Down Expand Up @@ -494,15 +502,15 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
try {
const pr = (await this.getProviderPullRequestForCommit(this._session!, ref)) ?? null;
this._prsByCommit.set(ref, pr);
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
return pr;
} catch (ex) {
Logger.error(ex, cc);

this._prsByCommit.delete(ref);

if (ex instanceof AuthenticationError) {
this.handleAuthenticationException();
if (ex instanceof ClientError || ex instanceof AuthenticationError) {
this.handleClientException();
}
return null;
}
Expand Down Expand Up @@ -548,7 +556,7 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
}

this._session = session ?? null;
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;

if (session != null) {
await Container.context.workspaceState.update(this.disallowConnectionKey, undefined);
Expand Down Expand Up @@ -595,10 +603,10 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
}

@debug()
private handleAuthenticationException() {
this.invalidAuthenticationCount++;
private handleClientException() {
this.invalidClientExceptionCount++;

if (this.invalidAuthenticationCount >= 5) {
if (this.invalidClientExceptionCount >= 5) {
this.disconnect();
}
}
Expand Down
27 changes: 16 additions & 11 deletions src/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { graphql } from '@octokit/graphql';
import { Logger } from '../logger';
import { debug, Functions } from '../system';
import { AuthenticationError, IssueOrPullRequest, PullRequest, PullRequestState } from '../git/git';
import { AuthenticationError, ClientError, IssueOrPullRequest, PullRequest, PullRequestState } from '../git/git';
import { Account } from '../git/models/author';

export class GitHubApi {
Expand Down Expand Up @@ -75,8 +75,9 @@ export class GitHubApi {
} catch (ex) {
Logger.error(ex, cc);

if (ex.code === 401) {
throw new AuthenticationError(ex);
if (ex.code >= 400 && ex.code <= 500) {
if (ex.code === 401) throw new AuthenticationError(ex);
throw new ClientError(ex);
}
throw ex;
}
Expand Down Expand Up @@ -147,8 +148,9 @@ export class GitHubApi {
} catch (ex) {
Logger.error(ex, cc);

if (ex.code === 401) {
throw new AuthenticationError(ex);
if (ex.code >= 400 && ex.code <= 500) {
if (ex.code === 401) throw new AuthenticationError(ex);
throw new ClientError(ex);
}
throw ex;
}
Expand Down Expand Up @@ -215,8 +217,9 @@ export class GitHubApi {
} catch (ex) {
Logger.error(ex, cc);

if (ex.code === 401) {
throw new AuthenticationError(ex);
if (ex.code >= 400 && ex.code <= 500) {
if (ex.code === 401) throw new AuthenticationError(ex);
throw new ClientError(ex);
}
throw ex;
}
Expand Down Expand Up @@ -313,8 +316,9 @@ export class GitHubApi {
} catch (ex) {
Logger.error(ex, cc);

if (ex.code === 401) {
throw new AuthenticationError(ex);
if (ex.code >= 400 && ex.code <= 500) {
if (ex.code === 401) throw new AuthenticationError(ex);
throw new ClientError(ex);
}
throw ex;
}
Expand Down Expand Up @@ -425,8 +429,9 @@ export class GitHubApi {
} catch (ex) {
Logger.error(ex, cc);

if (ex.code === 401) {
throw new AuthenticationError(ex);
if (ex.code >= 400 && ex.code <= 500) {
if (ex.code === 401) throw new AuthenticationError(ex);
throw new ClientError(ex);
}
throw ex;
}
Expand Down

0 comments on commit ac9bfa4

Please sign in to comment.