Skip to content

Commit

Permalink
feat: exposed entity and criteria properties on EntityNotFoundError (#…
Browse files Browse the repository at this point in the history
…10202)

These properties can be used by consumers to aid in rendering human-readable error messages.
  • Loading branch information
clintonb committed Dec 29, 2023
1 parent b53e410 commit bafcd17
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/error/EntityNotFoundError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import { InstanceChecker } from "../util/InstanceChecker"
* Thrown when no result could be found in methods which are not allowed to return undefined or an empty set.
*/
export class EntityNotFoundError extends TypeORMError {
public readonly entityClass: EntityTarget<any>
public readonly criteria: any

constructor(entityClass: EntityTarget<any>, criteria: any) {
super()

this.entityClass = entityClass
this.criteria = criteria

this.message =
`Could not find any entity of type "${this.stringifyTarget(
entityClass,
Expand Down
24 changes: 15 additions & 9 deletions test/functional/repository/find-methods/repostiory-find-methods.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "reflect-metadata"
import { expect } from "chai"
import { assert, expect } from "chai"
import {
closeTestingConnections,
createTestingConnections,
Expand Down Expand Up @@ -751,14 +751,20 @@ describe("repository > find methods", () => {
loadedUser!.firstName.should.be.equal("name #0")
loadedUser!.secondName.should.be.equal("Doe")

await userRepository
.findOneOrFail({
where: {
id: 1,
secondName: "Dorian",
},
})
.should.eventually.be.rejectedWith(EntityNotFoundError)
const options = {
where: {
id: 1,
secondName: "Dorian",
},
}
try {
await userRepository.findOneOrFail(options)
assert.fail("Should have thrown an error.")
} catch (err) {
expect(err).to.be.an.instanceOf(EntityNotFoundError)
expect(err).to.have.property("entityClass", "User")
expect(err).to.have.property("criteria", options)
}
}),
))

Expand Down

0 comments on commit bafcd17

Please sign in to comment.