From 1e8e3f9283243e600d6b0c875c0b7986d4af96d1 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 27 May 2024 13:38:47 +0200 Subject: [PATCH] events-backend: test for reading events without subscription + fix error Signed-off-by: Patrik Oldsberg --- .../src/service/EventsPlugin.test.ts | 14 ++++++++++++++ .../src/service/hub/MemoryEventBusStore.ts | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/plugins/events-backend/src/service/EventsPlugin.test.ts b/plugins/events-backend/src/service/EventsPlugin.test.ts index 215d4e9190..53f84ffed1 100644 --- a/plugins/events-backend/src/service/EventsPlugin.test.ts +++ b/plugins/events-backend/src/service/EventsPlugin.test.ts @@ -416,5 +416,19 @@ describe('eventsPlugin', () => { await backend.stop(); }, ); + + it.each(databases.eachSupportedId())( + 'should refuse listen without a subscription, %p', + async databaseId => { + const backend = await startTestBackend({ + features: [eventsPlugin(), await mockKnexFactory(databaseId)], + }); + const helper = new ReqHelper(backend); + + await helper.readEvents('nonexistent').expect(404); + + await backend.stop(); + }, + ); }); }); diff --git a/plugins/events-backend/src/service/hub/MemoryEventBusStore.ts b/plugins/events-backend/src/service/hub/MemoryEventBusStore.ts index 629c341a76..3071a51edf 100644 --- a/plugins/events-backend/src/service/hub/MemoryEventBusStore.ts +++ b/plugins/events-backend/src/service/hub/MemoryEventBusStore.ts @@ -15,6 +15,7 @@ */ import { EventParams } from '@backstage/plugin-events-node'; import { EventBusStore } from './types'; +import { NotFoundError } from '@backstage/errors'; const MAX_BATCH_SIZE = 10; @@ -80,7 +81,7 @@ export class MemoryEventBusStore implements EventBusStore { async readSubscription(id: string): Promise<{ events: EventParams[] }> { const sub = this.#subscribers.get(id); if (!sub) { - throw new Error(`Subscription not found`); + throw new NotFoundError(`Subscription not found`); } const events = this.#events .filter( @@ -108,7 +109,7 @@ export class MemoryEventBusStore implements EventBusStore { const sub = this.#subscribers.get(subscriptionId); if (!sub) { - throw new Error(`Subscription not found`); + throw new NotFoundError(`Subscription not found`); } return new Promise<{ topic: string }>((resolve, reject) => {