Merge pull request #12695 from backstage/blam/better-scaffolder-git-errors
backend-common: better errors for `git` operations
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/backend-common': patch
|
||||
---
|
||||
|
||||
Added some more information to the error messages for `isomorphic-git` errors
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -90,21 +90,30 @@ export class Git {
|
||||
}): Promise<void> {
|
||||
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<void> {
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user