Browse Source

Adds better error handling to remote providers

main
Eric Amodio 4 years ago
parent
commit
ac9bfa44a1
2 changed files with 45 additions and 32 deletions
  1. +29
    -21
      src/git/remotes/provider.ts
  2. +16
    -11
      src/github/github.ts

+ 29
- 21
src/git/remotes/provider.ts View File

@ -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 // TODO@eamodio revisit how once authenticated, all remotes are always connected, even after a restart
export abstract class RemoteProviderWithApi extends RemoteProvider { export abstract class RemoteProviderWithApi extends RemoteProvider {
@ -249,7 +257,7 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
return this._onDidChange.event; return this._onDidChange.event;
} }
private invalidAuthenticationCount = 0;
private invalidClientExceptionCount = 0;
constructor(domain: string, path: string, protocol?: string, name?: string, custom?: boolean) { constructor(domain: string, path: string, protocol?: string, name?: string, custom?: boolean) {
super(domain, path, protocol, name, custom); super(domain, path, protocol, name, custom);
@ -314,7 +322,7 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
disconnect(silent: boolean = false): void { disconnect(silent: boolean = false): void {
const disconnected = this._session != null; const disconnected = this._session != null;
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
this._prsByCommit.clear(); this._prsByCommit.clear();
this._session = null; this._session = null;
@ -351,13 +359,13 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
try { try {
const author = await this.getProviderAccountForCommit(this._session!, ref, options); const author = await this.getProviderAccountForCommit(this._session!, ref, options);
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
return author; return author;
} catch (ex) { } catch (ex) {
Logger.error(ex, cc); Logger.error(ex, cc);
if (ex instanceof AuthenticationError) {
this.handleAuthenticationException();
if (ex instanceof ClientError || ex instanceof AuthenticationError) {
this.handleClientException();
} }
return undefined; return undefined;
} }
@ -386,13 +394,13 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
try { try {
const author = await this.getProviderAccountForEmail(this._session!, email, options); const author = await this.getProviderAccountForEmail(this._session!, email, options);
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
return author; return author;
} catch (ex) { } catch (ex) {
Logger.error(ex, cc); Logger.error(ex, cc);
if (ex instanceof AuthenticationError) {
this.handleAuthenticationException();
if (ex instanceof ClientError || ex instanceof AuthenticationError) {
this.handleClientException();
} }
return undefined; return undefined;
} }
@ -416,13 +424,13 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
try { try {
const issueOrPullRequest = await this.getProviderIssueOrPullRequest(this._session!, id); const issueOrPullRequest = await this.getProviderIssueOrPullRequest(this._session!, id);
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
return issueOrPullRequest; return issueOrPullRequest;
} catch (ex) { } catch (ex) {
Logger.error(ex, cc); Logger.error(ex, cc);
if (ex instanceof AuthenticationError) {
this.handleAuthenticationException();
if (ex instanceof ClientError || ex instanceof AuthenticationError) {
this.handleClientException();
} }
return undefined; return undefined;
} }
@ -449,13 +457,13 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
try { try {
const pr = await this.getProviderPullRequestForBranch(this._session!, branch, options); const pr = await this.getProviderPullRequestForBranch(this._session!, branch, options);
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
return pr; return pr;
} catch (ex) { } catch (ex) {
Logger.error(ex, cc); Logger.error(ex, cc);
if (ex instanceof AuthenticationError) {
this.handleAuthenticationException();
if (ex instanceof ClientError || ex instanceof AuthenticationError) {
this.handleClientException();
} }
return undefined; return undefined;
} }
@ -494,15 +502,15 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
try { try {
const pr = (await this.getProviderPullRequestForCommit(this._session!, ref)) ?? null; const pr = (await this.getProviderPullRequestForCommit(this._session!, ref)) ?? null;
this._prsByCommit.set(ref, pr); this._prsByCommit.set(ref, pr);
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
return pr; return pr;
} catch (ex) { } catch (ex) {
Logger.error(ex, cc); Logger.error(ex, cc);
this._prsByCommit.delete(ref); this._prsByCommit.delete(ref);
if (ex instanceof AuthenticationError) {
this.handleAuthenticationException();
if (ex instanceof ClientError || ex instanceof AuthenticationError) {
this.handleClientException();
} }
return null; return null;
} }
@ -548,7 +556,7 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
} }
this._session = session ?? null; this._session = session ?? null;
this.invalidAuthenticationCount = 0;
this.invalidClientExceptionCount = 0;
if (session != null) { if (session != null) {
await Container.context.workspaceState.update(this.disallowConnectionKey, undefined); await Container.context.workspaceState.update(this.disallowConnectionKey, undefined);
@ -595,10 +603,10 @@ export abstract class RemoteProviderWithApi extends RemoteProvider {
} }
@debug() @debug()
private handleAuthenticationException() {
this.invalidAuthenticationCount++;
private handleClientException() {
this.invalidClientExceptionCount++;
if (this.invalidAuthenticationCount >= 5) {
if (this.invalidClientExceptionCount >= 5) {
this.disconnect(); this.disconnect();
} }
} }

+ 16
- 11
src/github/github.ts View File

@ -2,7 +2,7 @@
import { graphql } from '@octokit/graphql'; import { graphql } from '@octokit/graphql';
import { Logger } from '../logger'; import { Logger } from '../logger';
import { debug, Functions } from '../system'; 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'; import { Account } from '../git/models/author';
export class GitHubApi { export class GitHubApi {
@ -75,8 +75,9 @@ export class GitHubApi {
} catch (ex) { } catch (ex) {
Logger.error(ex, cc); 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; throw ex;
} }
@ -147,8 +148,9 @@ export class GitHubApi {
} catch (ex) { } catch (ex) {
Logger.error(ex, cc); 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; throw ex;
} }
@ -215,8 +217,9 @@ export class GitHubApi {
} catch (ex) { } catch (ex) {
Logger.error(ex, cc); 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; throw ex;
} }
@ -313,8 +316,9 @@ export class GitHubApi {
} catch (ex) { } catch (ex) {
Logger.error(ex, cc); 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; throw ex;
} }
@ -425,8 +429,9 @@ export class GitHubApi {
} catch (ex) { } catch (ex) {
Logger.error(ex, cc); 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; throw ex;
} }

Loading…
Cancel
Save