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.
This commit is contained in:
KernelDeimos
2026-01-20 16:28:22 -05:00
committed by Eric Dubé
parent 5b33beb6a1
commit 768a35e9b5
2 changed files with 19 additions and 12 deletions
@@ -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];
@@ -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, {