diff --git a/examples/todo-list/src/controllers/todo-list-image.controller.ts b/examples/todo-list/src/controllers/todo-list-image.controller.ts index d909f4b156bc..e9ff4915f105 100644 --- a/examples/todo-list/src/controllers/todo-list-image.controller.ts +++ b/examples/todo-list/src/controllers/todo-list-image.controller.ts @@ -1,6 +1,6 @@ import {TodoListRepository} from '../repositories'; import {repository} from '@loopback/repository'; -import {param, post, requestBody, get} from '@loopback/rest'; +import {param, post, requestBody, get, HttpErrors} from '@loopback/rest'; import {TodoListImage} from '../models'; export class TodoListImageController { @@ -32,6 +32,10 @@ export class TodoListImageController { }, }) async find(@param.path.number('id') id: number): Promise { - return await this.todoListRepo.image(id).get(); + const foundImage = await this.todoListRepo.image(id).get(); + if (!foundImage) { + throw new HttpErrors.NotFound(); + } + return foundImage; } } diff --git a/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts b/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts index 0343a41443e4..d650daec4d92 100644 --- a/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts +++ b/packages/repository/src/__tests__/acceptance/has-one.relation.acceptance.ts @@ -11,7 +11,6 @@ import { repository, RepositoryMixin, Filter, - EntityNotFoundError, } from '../..'; import {Address} from '../fixtures/models'; import {CustomerRepository, AddressRepository} from '../fixtures/repositories'; @@ -75,8 +74,9 @@ describe('hasOne relation', () => { const foundAddress = await controller.findCustomerAddress( existingCustomerId, ); - expect(foundAddress).to.containEql(address); - expect(toJSON(foundAddress)).to.deepEqual(toJSON(address)); + expect(foundAddress).to.not.be.null(); + expect(foundAddress!).to.containEql(address); + expect(toJSON(foundAddress!)).to.deepEqual(toJSON(address)); const persisted = await addressRepo.find({ where: {customerId: existingCustomerId}, @@ -104,15 +104,16 @@ describe('hasOne relation', () => { expect(persisted[0]).to.deepEqual(foundAddress); }); - it('reports EntityNotFound error when related model is deleted', async () => { + it('returns null when related model is deleted', async () => { const address = await controller.createCustomerAddress(existingCustomerId, { street: '123 test avenue', }); await addressRepo.deleteById(address.zipcode); - await expect( - controller.findCustomerAddress(existingCustomerId), - ).to.be.rejectedWith(EntityNotFoundError); + const foundAddress = await controller.findCustomerAddress( + existingCustomerId, + ); + expect(foundAddress).to.be.null(); }); /*---------------- HELPERS -----------------*/ diff --git a/packages/repository/src/relations/has-one/has-one.repository.ts b/packages/repository/src/relations/has-one/has-one.repository.ts index 961e39e25f24..fc2a8f6251c8 100644 --- a/packages/repository/src/relations/has-one/has-one.repository.ts +++ b/packages/repository/src/relations/has-one/has-one.repository.ts @@ -12,7 +12,6 @@ import { constrainFilter, EntityCrudRepository, } from '../../repositories'; -import {EntityNotFoundError} from '../../errors'; /** * CRUD operations for a target repository of a HasMany relation @@ -38,7 +37,7 @@ export interface HasOneRepository { get( filter?: Pick, Exclude, 'where'>>, options?: Options, - ): Promise; + ): Promise; } export class DefaultHasOneRepository< @@ -74,16 +73,14 @@ export class DefaultHasOneRepository< Exclude, 'where'> >, options?: Options, - ): Promise { + ): Promise { const targetRepository = await this.getTargetRepository(); const found = await targetRepository.find( Object.assign({limit: 1}, constrainFilter(filter, this.constraint)), options, ); if (found.length < 1) { - // We don't have a direct access to the foreign key value here :( - const id = 'constraint ' + JSON.stringify(this.constraint); - throw new EntityNotFoundError(targetRepository.entityClass, id); + return null; } return found[0]; }