Skip to content

Commit 04077dc

Browse files
author
Kevin Delisle
committed
fix(repository): findById will reject on no result
findById will now reject when no result is found, instead of returning null.
1 parent 3def7c7 commit 04077dc

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

packages/repository/src/legacy-juggler-bridge.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ export class DefaultCrudRepository<T extends Entity, ID>
136136
const model = await ensurePromise(
137137
this.modelClass.findById(id, filter, options),
138138
);
139+
if (!model) {
140+
return Promise.reject(
141+
new Error(`no ${this.modelClass.name} found with id "${id}"`),
142+
);
143+
}
139144
return this.toEntity(model);
140145
}
141146

packages/repository/test/unit/repository/legacy-juggler-bridge.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,15 @@ describe('DefaultCrudRepository', () => {
180180
const ok = await repo.exists(note1.id);
181181
expect(ok).to.be.true();
182182
});
183+
184+
it('throws if findById does not return a value', async () => {
185+
const repo = new DefaultCrudRepository(Note, ds);
186+
try {
187+
await repo.findById(999999);
188+
} catch (err) {
189+
expect(err).to.match(/no Note was found with id/);
190+
return;
191+
}
192+
throw new Error('No error was returned!');
193+
});
183194
});

0 commit comments

Comments
 (0)