From 768a35e9b58219c41d4a2fad4f683cee09413812 Mon Sep 17 00:00:00 2001 From: KernelDeimos <7225168+KernelDeimos@users.noreply.github.com> Date: Tue, 13 Jan 2026 14:42:33 -0500 Subject: [PATCH] fix(data-access): read response regression In `es:app`, attempting to read an app that doesn't exist results in an error being thrown. This commit updates `app` to have the same behavior. --- .../src/modules/data-access/AppService.js | 19 ++++++++++++++----- .../modules/data-access/AppService.test.js | 12 +++++------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/backend/src/modules/data-access/AppService.js b/src/backend/src/modules/data-access/AppService.js index ecaeeb5b9..70859ecb6 100644 --- a/src/backend/src/modules/data-access/AppService.js +++ b/src/backend/src/modules/data-access/AppService.js @@ -51,10 +51,17 @@ export default class AppService extends BaseService { let existing = null; if ( object.uid !== undefined || id !== undefined ) { - existing = await this.#read({ - uid: object.uid, - id, - }); + try { + existing = await this.#read({ + uid: object.uid, + id, + }); + } catch ( error ) { + // If entity not found, we'll create it + if ( error.fields?.code !== 'entity_not_found' ) { + throw error; + } + } } if ( existing ) { @@ -234,7 +241,9 @@ export default class AppService extends BaseService { const rows = await db.read(stmt, whereValues); if ( rows.length === 0 ) { - return undefined; + throw APIError.create('entity_not_found', null, { + identifier: uid || JSON.stringify(id), + }); } const row = rows[0]; diff --git a/src/backend/src/modules/data-access/AppService.test.js b/src/backend/src/modules/data-access/AppService.test.js index 9b5775d60..8aaa25f5d 100644 --- a/src/backend/src/modules/data-access/AppService.test.js +++ b/src/backend/src/modules/data-access/AppService.test.js @@ -217,13 +217,14 @@ describe('AppService', () => { expect(result.name).toBe('test-app'); }); - it('should return undefined when no app is found', async () => { + it('should throw entity_not_found when no app is found', async () => { mockDb.read.mockResolvedValue([]); const crudQ = AppService.IMPLEMENTS['crud-q']; - const result = await crudQ.read.call(appService, { uid: 'nonexistent-uid' }); - expect(result).toBeUndefined(); + await expect(crudQ.read.call(appService, { uid: 'nonexistent-uid' })).rejects.toMatchObject({ + fields: { code: 'entity_not_found' }, + }); }); it('should throw an error when neither uid nor id is provided', async () => { @@ -1257,10 +1258,7 @@ describe('AppService', () => { it('should call create when entity does not exist', async () => { setupContextForWrite(createMockUserActor(1)); - // First read returns empty (entity doesn't exist) - mockDb.read - .mockResolvedValueOnce([]) // lookup - .mockResolvedValue([createMockAppRow()]); // read after create + mockDb.read.mockResolvedValue([createMockAppRow()]); const crudQ = AppService.IMPLEMENTS['crud-q']; await crudQ.upsert.call(appService, {