From 840a2687fe903d99308a1c7f956548b6fa8a2850 Mon Sep 17 00:00:00 2001 From: Alex Lorenzi Date: Wed, 12 Feb 2025 18:22:44 -0500 Subject: [PATCH] This is a better fix. Docs for Azure/fetch-event-source mention you should re-throw the error in the onerror function to stop operation Signed-off-by: Alex Lorenzi --- .../src/service/router.test.ts | 18 ++---------------- plugins/techdocs-backend/src/service/router.ts | 12 ++++-------- plugins/techdocs/src/client.ts | 1 + 3 files changed, 7 insertions(+), 24 deletions(-) diff --git a/plugins/techdocs-backend/src/service/router.test.ts b/plugins/techdocs-backend/src/service/router.test.ts index 21b6dd5554..c74167552d 100644 --- a/plugins/techdocs-backend/src/service/router.test.ts +++ b/plugins/techdocs-backend/src/service/router.test.ts @@ -164,14 +164,7 @@ describe('createRouter', () => { .set('accept', 'text/event-stream') .send(); - expect(response.status).toBe(200); - expect(response.get('content-type')).toBe('text/event-stream'); - expect(response.text).toEqual( - `event: error -data: "Entity not found" - -`, - ); + expect(response.status).toBe(404); }); it('should return not found if entity has no uid', async () => { @@ -186,14 +179,7 @@ data: "Entity not found" .set('accept', 'text/event-stream') .send(); - expect(response.status).toBe(200); - expect(response.get('content-type')).toBe('text/event-stream'); - expect(response.text).toEqual( - `event: error -data: "Entity metadata UID missing" - -`, - ); + expect(response.status).toBe(404); }); it('should not check for an update when shouldBuild returns false', async () => { diff --git a/plugins/techdocs-backend/src/service/router.ts b/plugins/techdocs-backend/src/service/router.ts index 301bc43755..b99579e0eb 100644 --- a/plugins/techdocs-backend/src/service/router.ts +++ b/plugins/techdocs-backend/src/service/router.ts @@ -242,18 +242,14 @@ export async function createRouter( targetPluginId: 'catalog', }); - const responseHandler: DocsSynchronizerSyncOpts = createEventStream(res); - const entity = await entityLoader.load({ kind, namespace, name }, token); + if (!entity?.metadata?.uid) { - responseHandler.error( - new NotFoundError( - entity ? 'Entity metadata UID missing' : 'Entity not found', - ), - ); - return; + throw new NotFoundError('Entity metadata UID missing'); } + const responseHandler: DocsSynchronizerSyncOpts = createEventStream(res); + // By default, techdocs-backend will only try to build documentation for an entity if techdocs.builder is set to // 'local'. If set to 'external', it will assume that an external process (e.g. CI/CD pipeline // of the repository) is responsible for building and publishing documentation to the storage provider. diff --git a/plugins/techdocs/src/client.ts b/plugins/techdocs/src/client.ts index d00be2a01f..9ea753b1e6 100644 --- a/plugins/techdocs/src/client.ts +++ b/plugins/techdocs/src/client.ts @@ -236,6 +236,7 @@ export class TechDocsStorageClient implements TechDocsStorageApi { onerror(err) { ctrl.abort(); reject(err); + throw err; // rethrow to stop the operation }, }); });