forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityNotFoundError.ts
More file actions
43 lines (37 loc) · 1.4 KB
/
EntityNotFoundError.ts
File metadata and controls
43 lines (37 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { EntityTarget } from "../common/EntityTarget"
import { TypeORMError } from "./TypeORMError"
import { ObjectUtils } from "../util/ObjectUtils"
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,
)}" ` + `matching: ${this.stringifyCriteria(criteria)}`
}
private stringifyTarget(target: EntityTarget<any>): string {
if (InstanceChecker.isEntitySchema(target)) {
return target.options.name
} else if (typeof target === "function") {
return target.name
} else if (ObjectUtils.isObject(target) && "name" in (target as any)) {
return (target as any).name
} else {
return target as any
}
}
private stringifyCriteria(criteria: any): string {
try {
return JSON.stringify(criteria, null, 4)
} catch (e) {}
return "" + criteria
}
}