From 14f6f11eecc0f836dc8645bfa2ee3312933275ff Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 18 Jul 2022 14:30:12 +0200 Subject: [PATCH] feat: added some more information to the error for the user Signed-off-by: blam --- packages/backend-common/src/scm/git.test.ts | 63 ++++++++++++ packages/backend-common/src/scm/git.ts | 100 +++++++++++++------- 2 files changed, 128 insertions(+), 35 deletions(-) diff --git a/packages/backend-common/src/scm/git.test.ts b/packages/backend-common/src/scm/git.test.ts index 46aa0804b6..7b366e01c8 100644 --- a/packages/backend-common/src/scm/git.test.ts +++ b/packages/backend-common/src/scm/git.test.ts @@ -129,6 +129,27 @@ describe('Git', () => { expect(onAuth()).toEqual(auth); }); + + it('should propogate the data from the error handler', async () => { + const url = 'http://github.com/some/repo'; + const dir = '/some/mock/dir'; + const auth = { + username: 'blob', + password: 'hunter2', + }; + const git = Git.fromAuth(auth); + + (isomorphic.clone as jest.Mock).mockImplementation(() => { + const error: Error & { data?: unknown } = new Error('mock error'); + error.data = { some: 'more information here' }; + + throw error; + }); + + await expect(git.clone({ url, dir })).rejects.toThrow( + 'more information here', + ); + }); }); describe('currentBranch', () => { @@ -196,6 +217,27 @@ describe('Git', () => { expect(onAuth()).toEqual(auth); }); + + it('should propogate the data from the error handler', async () => { + const remote = 'http://github.com/some/repo'; + const dir = '/some/mock/dir'; + const auth = { + username: 'blob', + password: 'hunter2', + }; + const git = Git.fromAuth(auth); + + (isomorphic.fetch as jest.Mock).mockImplementation(() => { + const error: Error & { data?: unknown } = new Error('mock error'); + error.data = { some: 'more information here' }; + + throw error; + }); + + await expect(git.fetch({ remote, dir })).rejects.toThrow( + 'more information here', + ); + }); }); describe('init', () => { @@ -285,6 +327,27 @@ describe('Git', () => { expect(onAuth()).toEqual(auth); }); + + it('should propogate the data from the error handler', async () => { + const remote = 'origin'; + const dir = '/some/mock/dir'; + const auth = { + username: 'blob', + password: 'hunter2', + }; + const git = Git.fromAuth(auth); + + (isomorphic.push as jest.Mock).mockImplementation(() => { + const error: Error & { data?: unknown } = new Error('mock error'); + error.data = { some: 'more information here' }; + + throw error; + }); + + await expect(git.push({ remote, dir })).rejects.toThrow( + 'more information here', + ); + }); }); describe('readCommit', () => { diff --git a/packages/backend-common/src/scm/git.ts b/packages/backend-common/src/scm/git.ts index 9cf84e9872..e76f675821 100644 --- a/packages/backend-common/src/scm/git.ts +++ b/packages/backend-common/src/scm/git.ts @@ -90,21 +90,30 @@ export class Git { }): Promise { const { url, dir, ref, depth, noCheckout } = options; this.config.logger?.info(`Cloning repo {dir=${dir},url=${url}}`); - return git.clone({ - fs, - http, - url, - dir, - ref, - singleBranch: true, - depth: depth ?? 1, - noCheckout, - onProgress: this.onProgressHandler(), - headers: { - 'user-agent': 'git/@isomorphic-git', - }, - onAuth: this.onAuth, - }); + + try { + return await git.clone({ + fs, + http, + url, + dir, + ref, + singleBranch: true, + depth: depth ?? 1, + noCheckout, + onProgress: this.onProgressHandler(), + headers: { + 'user-agent': 'git/@isomorphic-git', + }, + onAuth: this.onAuth, + }); + } catch (ex) { + this.config.logger?.error(`Failed to clone repo {dir=${dir},url=${url}}`); + if (ex.data) { + throw new Error(`${ex.message} {data=${JSON.stringify(ex.data)}}`); + } + throw ex; + } } /** https://isomorphic-git.org/docs/en/currentBranch */ @@ -124,15 +133,26 @@ export class Git { this.config.logger?.info( `Fetching remote=${remote} for repository {dir=${dir}}`, ); - await git.fetch({ - fs, - http, - dir, - remote, - onProgress: this.onProgressHandler(), - headers: { 'user-agent': 'git/@isomorphic-git' }, - onAuth: this.onAuth, - }); + + try { + await git.fetch({ + fs, + http, + dir, + remote, + onProgress: this.onProgressHandler(), + headers: { 'user-agent': 'git/@isomorphic-git' }, + onAuth: this.onAuth, + }); + } catch (ex) { + this.config.logger?.error( + `Failed to fetch repo {dir=${dir},origin=${origin}}`, + ); + if (ex.data) { + throw new Error(`${ex.message} {data=${JSON.stringify(ex.data)}}`); + } + throw ex; + } } async init(options: { dir: string; defaultBranch?: string }): Promise { @@ -175,17 +195,27 @@ export class Git { this.config.logger?.info( `Pushing directory to remote {dir=${dir},remote=${remote}}`, ); - return git.push({ - fs, - dir, - http, - onProgress: this.onProgressHandler(), - headers: { - 'user-agent': 'git/@isomorphic-git', - }, - remote: remote, - onAuth: this.onAuth, - }); + try { + return await git.push({ + fs, + dir, + http, + onProgress: this.onProgressHandler(), + headers: { + 'user-agent': 'git/@isomorphic-git', + }, + remote: remote, + onAuth: this.onAuth, + }); + } catch (ex) { + this.config.logger?.error( + `Failed to push to repo {dir=${dir}, remote=${remote}}`, + ); + if (ex.data) { + throw new Error(`${ex.message} {data=${JSON.stringify(ex.data)}}`); + } + throw ex; + } } /** https://isomorphic-git.org/docs/en/readCommit */