From cc4c2baf97b5827465768f51bb6f22f0653faaa9 Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Wed, 20 Nov 2024 22:05:21 +0100 Subject: [PATCH 1/2] catalog-client: fix queryEntities not handling errors properly Signed-off-by: Vincenzo Scamporlino --- packages/catalog-client/src/CatalogClient.test.ts | 12 ++++++++++++ packages/catalog-client/src/CatalogClient.ts | 6 +++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/catalog-client/src/CatalogClient.test.ts b/packages/catalog-client/src/CatalogClient.test.ts index ce44b93b59..ea61634a8b 100644 --- a/packages/catalog-client/src/CatalogClient.test.ts +++ b/packages/catalog-client/src/CatalogClient.test.ts @@ -525,6 +525,18 @@ describe('CatalogClient', () => { '?cursor=prevcursor', ); }); + + it('should handle errors', async () => { + const mockedEndpoint = jest + .fn() + .mockImplementation((_req, res, ctx) => res(ctx.status(401))); + + server.use(rest.get(`${mockBaseUrl}/entities/by-query`, mockedEndpoint)); + + await expect(() => client.queryEntities()).rejects.toThrow( + /Request failed with 401 Unauthorized/, + ); + }); }); describe('getEntityByRef', () => { diff --git a/packages/catalog-client/src/CatalogClient.ts b/packages/catalog-client/src/CatalogClient.ts index 8054063c0b..e820716945 100644 --- a/packages/catalog-client/src/CatalogClient.ts +++ b/packages/catalog-client/src/CatalogClient.ts @@ -237,9 +237,9 @@ export class CatalogClient implements CatalogApi { } } - return this.apiClient - .getEntitiesByQuery({ query: params }, options) - .then(r => r.json()); + return this.requestRequired( + await this.apiClient.getEntitiesByQuery({ query: params }, options), + ); } /** From d7e7836eec21024c34ba94a1e1989682d8bfeb3e Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Wed, 20 Nov 2024 22:07:04 +0100 Subject: [PATCH 2/2] catalog-client: changeset Signed-off-by: Vincenzo Scamporlino --- .changeset/swift-kangaroos-drop.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/swift-kangaroos-drop.md diff --git a/.changeset/swift-kangaroos-drop.md b/.changeset/swift-kangaroos-drop.md new file mode 100644 index 0000000000..77d837deba --- /dev/null +++ b/.changeset/swift-kangaroos-drop.md @@ -0,0 +1,5 @@ +--- +'@backstage/catalog-client': patch +--- + +Fixed a bug in the `queryEntities` method where errors were not being handled properly.