mirror of
https://github.com/HeyPuter/puter.git
synced 2026-09-24 22:26:42 +00:00
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:
@@ -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, {
|
||||
|
||||
Reference in New Issue
Block a user