Skip to content

Commit

Permalink
fix: add catch to azure api calls and resturcture functions
Browse files Browse the repository at this point in the history
  • Loading branch information
IlanTSnyk committed Jan 18, 2022
1 parent 393e6ac commit acb8715
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/lib/source-handlers/azure/list-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,27 @@ async function fetchAllProjects(
let nextPage: string | undefined;
while (hasMorePages) {
debug(`Fetching page ${displayPageNumber} for Azure projects`);
const { projects, continueFrom } = await getProjects(
orgName,
host,
nextPage,
);
if (continueFrom && projects.length > 0) {
nextPage = continueFrom;
displayPageNumber++;
} else {
hasMorePages = false;
}
projects.map((project) => {
const { name, id } = project;
if (name && id) {
projectList.push({ id, name });
try {
const { projects, continueFrom } = await getProjects(
orgName,
host,
nextPage,
);
if (continueFrom && projects.length > 0) {
nextPage = continueFrom;
displayPageNumber++;
} else {
hasMorePages = false;
}
});
projects.map((project) => {
const { name, id } = project;
if (name && id) {
projectList.push({ id, name });
}
});
} catch (err) {
throw new Error(JSON.stringify(err));
}
}
return projectList;
}
Expand Down
15 changes: 15 additions & 0 deletions src/lib/source-handlers/azure/list-repos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ export async function fetchAllRepos(
token: string,
): Promise<AzureRepoData[]> {
debug('Fetching repos for ' + project);
let repoList: AzureRepoData[] = [];
try {
repoList = await getRepos(url, orgName, project, token);
} catch (err) {
throw new Error(JSON.stringify(err));
}
return repoList;
}

async function getRepos(
url: string,
orgName: string,
project: string,
token: string,
): Promise<AzureRepoData[]> {
const repoList: AzureRepoData[] = [];
const data = await limiter.schedule(() =>
needle(
Expand Down
5 changes: 5 additions & 0 deletions test/lib/source-handlers/azure/azure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ describe('Testing azure-devops interaction', () => {
const repos = await listAzureRepos('reposTestOrg', 'https://azure-tests');
expect(repos).toHaveLength(3);
});
test('listAzureRepos to fail', async () => {
expect(async () => {
await listAzureRepos('non-existing-org', 'https://non-existing-url')}
).rejects.toThrow();
});
});

0 comments on commit acb8715

Please sign in to comment.