瀏覽代碼

Adds better error handling to remote providers

main
Eric Amodio 4 年之前
父節點
當前提交
ac9bfa44a1
共有 2 個檔案被更改,包括 45 行新增32 行删除
  1. +29
    -21
      src/git/remotes/provider.ts
  2. +16
    -11
      src/github/github.ts

+ 29
- 21
src/git/remotes/provider.ts 查看文件

@ -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 {
@ -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);
@ -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;
@ -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;
}
@ -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;
}
@ -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;
}
@ -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;
}
@ -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;
}
@ -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);
@ -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();
}
}

+ 16
- 11
src/github/github.ts 查看文件

@ -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 {
@ -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;
}
@ -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;
}
@ -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;
}
@ -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;
}
@ -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;
}

Loading…
取消
儲存