From c35eebedad9d0992f5cd6fedb90503c7ef3ad965 Mon Sep 17 00:00:00 2001 From: Jamie Klassen Date: Tue, 14 Mar 2023 13:08:24 -0400 Subject: [PATCH] add light error handling to getGroupMembers Signed-off-by: Jamie Klassen --- .../src/lib/client.test.ts | 62 +++++++++++++------ .../src/lib/client.ts | 4 +- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts b/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts index 66c04f6cc4..0a50742a27 100644 --- a/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts +++ b/plugins/catalog-backend-module-gitlab/src/lib/client.test.ts @@ -398,30 +398,54 @@ describe('GitLabClient', () => { ]); }); - it('getGroupMembers gets member IDs', async () => { - server.use( - graphql - .link(`${MOCK_CONFIG.baseUrl}/api/graphql`) - .operation((_, res, ctx) => - res( - ctx.data({ - group: { - groupMembers: { - nodes: [{ user: { id: 'gid://gitlab/User/1' } }], + describe('getGroupMembers', () => { + it('gets member IDs', async () => { + server.use( + graphql + .link(`${MOCK_CONFIG.baseUrl}/api/graphql`) + .operation((_, res, ctx) => + res( + ctx.data({ + group: { + groupMembers: { + nodes: [{ user: { id: 'gid://gitlab/User/1' } }], + }, }, - }, - }), + }), + ), ), - ), - ); - const client = new GitLabClient({ - config: MOCK_CONFIG, - logger: getVoidLogger(), + ); + const client = new GitLabClient({ + config: MOCK_CONFIG, + logger: getVoidLogger(), + }); + + const members = await client.getGroupMembers('group1'); + + expect(members).toEqual([1]); }); - const members = await client.getGroupMembers('group1'); + it('rejects when GraphQL returns errors', async () => { + server.use( + graphql + .link(`${MOCK_CONFIG.baseUrl}/api/graphql`) + .operation((_, res, ctx) => + res( + ctx.errors([ + { message: 'Unexpected end of document', locations: [] }, + ]), + ), + ), + ); + const client = new GitLabClient({ + config: MOCK_CONFIG, + logger: getVoidLogger(), + }); - expect(members).toEqual([1]); + await expect(() => client.getGroupMembers('group1')).rejects.toThrow( + 'GraphQL errors: [{"message":"Unexpected end of document","locations":[]}]', + ); + }); }); }); diff --git a/plugins/catalog-backend-module-gitlab/src/lib/client.ts b/plugins/catalog-backend-module-gitlab/src/lib/client.ts index 74a3a4be71..a2d8ec971a 100644 --- a/plugins/catalog-backend-module-gitlab/src/lib/client.ts +++ b/plugins/catalog-backend-module-gitlab/src/lib/client.ts @@ -116,7 +116,9 @@ export class GitLabClient { }), }, ).then(r => r.json()); - this.logger.debug(`got GraphQL response: ${JSON.stringify(response)}`); + if (response.errors) { + throw new Error(`GraphQL errors: ${JSON.stringify(response.errors)}`); + } return response.data.group.groupMembers.nodes.map( (node: { user: { id: string } }) =>