Skip to content

Commit

Permalink
fix: Give proper error message when found cross reference entity issue (
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed May 23, 2021
1 parent 6682015 commit 0d09d22
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/core/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ namespace entityHelper {
if (!prop)
throw new Error(`${entity.name} doesn't have property named ${relation}`)
if (prop.type === Array && !prop.type[0])
throw new Error(errorMessage.GenericControllerMissingTypeInfo.format(`${entity.name}.${relation}`))
throw new Error(errorMessage.UnableToGetMemberDataType.format(entity.name, prop.name))
const type = Array.isArray(prop.type) ? "OneToMany" : "ManyToOne"
if (type === "OneToMany") {
const relDecorator: RelationDecorator = prop.decorators.find((x: RelationDecorator) => x.kind === "plumier-meta:relation")
Expand All @@ -265,6 +265,8 @@ namespace entityHelper {
}
else {
const parent: Class = prop.type
if (!parent)
throw new Error(errorMessage.UnableToGetMemberDataType.format(entity.name, prop.name))
const parentMeta = reflect(parent)
let parentProperty: string | undefined
for (const prop of parentMeta.properties) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ export namespace errorMessage {
export const CustomRouteRequiredTwoParameters = "Nested custom route path '{0}' on {1} entity, must have two route parameters, example: users/:userId/animals/:animalId"
export const CustomRouteMustHaveOneParameter = "Custom route path '{0}' on {1} entity, must have one route parameter, example: animals/:animalId"
export const EntityRequireID = "Entity {0} used by generic controller doesn't have an ID property"
export const UnableToGetMemberDataType = "Unable to get data type of member {0}.{1}. Make sure to provide type information, or manage if its has cross reference to other class"

//PLUM2XXX internal app error
export const UnableToInstantiateModel = `Unable to instantiate {0}. Domain model should not throw error inside constructor`
Expand Down
4 changes: 4 additions & 0 deletions tests/behavior/common/__snapshots__/common.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Object {
}
`;

exports[`Entity Relation Info Should throw error when no type information found on many to one relation 1`] = `"Unable to get data type of member Animal.user. Make sure to provide type information, or manage if its has cross reference to other class"`;

exports[`Entity Relation Info Should throw error when no type information found on one to many relation 1`] = `"Unable to get data type of member User.animals. Make sure to provide type information, or manage if its has cross reference to other class"`;

exports[`Entity Relation Info Should throw error when provided invalid property name 1`] = `"Animal doesn't have property named users"`;

exports[`Entity Relation Info Should throw error when provided non relation one to many property 1`] = `"User.animals is not a valid relation, make sure its decorated with @entity.relation() decorator"`;
Expand Down
43 changes: 43 additions & 0 deletions tests/behavior/common/common.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,49 @@ describe("Entity Relation Info", () => {
}
expect(() => entityHelper.getRelationInfo([Animal, "users"])).toThrowErrorMatchingSnapshot()
})
it("Should throw error when no type information found on one to many relation", () => {
@domain()
class User {
@entity.primaryId()
public id: number
@noop()
public name: string
@entity.relation()
public animals: Animal[]
}
@domain()
class Animal {
@entity.primaryId()
public id: number
@noop()
public name: string
@entity.relation()
public user: User
}
expect(() => entityHelper.getRelationInfo([User, "animals"])).toThrowErrorMatchingSnapshot()
})
it("Should throw error when no type information found on many to one relation", () => {
@domain()
class User {
@entity.primaryId()
public id: number
@noop()
public name: string
@entity.relation()
public animals: Animal[]
}
@domain()
class Animal {
@entity.primaryId()
public id: number
@noop()
public name: string
@entity.relation()
@meta.type(undefined as any)
public user: User
}
expect(() => entityHelper.getRelationInfo([Animal, "user"])).toThrowErrorMatchingSnapshot()
})
})

describe("Meta Decorator", () => {
Expand Down
1 change: 1 addition & 0 deletions tests/behavior/export/__snapshots__/export.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ Object {
"PropertyWithoutTypeInformation": "Parameter binding skipped because property doesn't have type information in ({0})",
"RouteDoesNotHaveBackingParam": "Route parameters ({0}) doesn't have appropriate backing parameter",
"UnableToConvertValue": "Unable to convert \\"{0}\\" into {1}",
"UnableToGetMemberDataType": "Unable to get data type of member {0}.{1}. Make sure to provide type information, or manage if its has cross reference to other class",
"UnableToInstantiateModel": "Unable to instantiate {0}. Domain model should not throw error inside constructor",
},
"executeAuthorizer": executeAuthorizer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2810,7 +2810,7 @@ Array [
exports[`Route Generator One To Many Controller Should throw error when the relation doesn't have type information 1`] = `
Array [
Array [
[Error: User.animals marked with @genericController() but doesn't have type information],
[Error: Unable to get data type of member User.animals. Make sure to provide type information, or manage if its has cross reference to other class],
],
]
`;
Expand Down

0 comments on commit 0d09d22

Please sign in to comment.