From 4a4f14f69b83857bafe32c0a12dc5cf6adeb4b70 Mon Sep 17 00:00:00 2001 From: Thomas Heartman Date: Fri, 30 Jun 2023 11:02:24 +0200 Subject: [PATCH] ux: return better error message if a segment doesn't exist (#4122) Catch cases where the segment doesn't exist and populate that error message with more info: it now says that a segment with doesn't exist instead of just 'No row'. --- src/lib/db/segment-store.test.ts | 30 ++++++++++++++++++++++++++++++ src/lib/db/segment-store.ts | 7 ++++++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/lib/db/segment-store.test.ts diff --git a/src/lib/db/segment-store.test.ts b/src/lib/db/segment-store.test.ts new file mode 100644 index 0000000000..875f4b9efd --- /dev/null +++ b/src/lib/db/segment-store.test.ts @@ -0,0 +1,30 @@ +import { ISegmentStore } from '../types/stores/segment-store'; +import dbInit from '../../test/e2e/helpers/database-init'; +import getLogger from '../../test/fixtures/no-logger'; +import NotFoundError from '../error/notfound-error'; + +let stores; +let db; +let segmentStore: ISegmentStore; + +beforeAll(async () => { + db = await dbInit('segment_store_serial', getLogger); + stores = db.stores; + segmentStore = stores.segmentStore; +}); + +afterAll(async () => { + await db.destroy(); +}); + +describe('unexpected input handling for get segment', () => { + test("gives a NotFoundError with the ID of the segment if it doesn't exist", async () => { + const id = 123; + try { + await segmentStore.get(id); + } catch (e) { + expect(e instanceof NotFoundError).toBeTruthy(); + expect(e.message).toEqual(expect.stringMatching(id.toString())); + } + }); +}); diff --git a/src/lib/db/segment-store.ts b/src/lib/db/segment-store.ts index a44835cfd0..2a88d6d6a9 100644 --- a/src/lib/db/segment-store.ts +++ b/src/lib/db/segment-store.ts @@ -195,7 +195,12 @@ export default class SegmentStore implements ISegmentStore { .from(T.segments) .where({ id }); - return this.mapRow(rows[0]); + const row = rows[0]; + if (!row) { + throw new NotFoundError(`No segment exists with ID "${id}"`); + } + + return this.mapRow(row); } async addToStrategy(id: number, strategyId: string): Promise {