Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw a better error when no record is found for running action on #1249

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export default class ActionContext<
*/
async getRecordIds(): Promise<Array<string | number>> {
const compositeIds = await this.getCompositeRecordIds();
this.assertSomeRecordsMatch(compositeIds);

return compositeIds.map(id => id[0]);
}
Expand Down Expand Up @@ -156,4 +157,9 @@ export default class ActionContext<
this.queries = [];
this.projection = new Projection();
}

protected assertSomeRecordsMatch(records) {
// The record might have been deleted since then
if (!records?.length) throw new Error('Query with filter did not match any records');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ export default class ActionContextSingle<
> extends ActionContext<S, N> {
async getRecordId(): Promise<string | number> {
const compositeId = await this.getCompositeRecordId();
this.assertSomeRecordsMatch(compositeId);

return compositeId[0];
}

async getCompositeRecordId(): Promise<CompositeId> {
const ids = await this.getCompositeRecordIds();
this.assertSomeRecordsMatch(ids);

return ids[0];
}

async getRecord(fields: TFieldName<S, N>[]): Promise<TRow<S, N>> {
const records = await this.getRecords(fields);
this.assertSomeRecordsMatch(records);

return records[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,15 @@ describe('ActionContext', () => {
expect(title).toEqual('Foundation');
expect(authorFirstName).toEqual('Isaac');
});

test('should throw a specific error is there are no records in the action context', async () => {
const caller = factories.caller.build();
(books.list as jest.Mock).mockResolvedValue([]);

const context = new ActionContextSingle(books, caller, {}, {});

await expect(context.getRecordIds()).rejects.toThrow(
'Query with filter did not match any records',
);
});
});
Loading