Skip to content

Commit 73999c9

Browse files
committed
Extract error messages to separate error classes
1 parent 891a724 commit 73999c9

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

src/decorators/OrmRepository.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { ConnectionManager, Repository, TreeRepository, MongoRepository } from "typeorm";
22
import { Container } from "typedi";
33

4+
import { EntityTypeMissingError } from "../errors/EntityTypeMissingError";
5+
import { PropertyTypeMissingError } from "../errors/PropertyTypeMissingError";
6+
import { ParamTypeMissingError } from "../errors/ParamTypeMissingError";
7+
48
/**
59
* Helper to avoid V8 compilation of anonymous function on each call of decorator.
610
*/
@@ -126,27 +130,15 @@ export function OrmRepository(entityTypeOrConnectionName?: Function|string, para
126130
if (index !== undefined) {
127131
const paramTypes: Function[] | undefined = Reflect.getOwnMetadata("design:paramtypes", object, propertyName);
128132
if (!paramTypes || !paramTypes[index]) {
129-
throw new Error(
130-
`Cannot get reflected type for a "${propertyName}" method's ${index + 1}. parameter of ${object.constructor.name} class. ` +
131-
`Make sure you have turned on an "emitDecoratorMetadata": true, option in tsconfig.json. ` +
132-
`and that you have imported "reflect-metadata" on top of the main entry file in your application.` +
133-
`And make sure that you have annotated the property type correctly with: ` +
134-
`Repository, MongoRepository, TreeRepository or custom repository class type.`
135-
);
133+
throw new ParamTypeMissingError(object, propertyName, index);
136134
}
137135
repositoryType = paramTypes[index];
138136
}
139137
// if the parameter has been aplied to class property
140138
else {
141139
const propertyType: Function | undefined = Reflect.getOwnMetadata("design:type", object, propertyName);
142140
if (!propertyType) {
143-
throw new Error(
144-
`Cannot get reflected type for a property "${propertyName}" of ${object.constructor.name} class. ` +
145-
`Make sure you have turned on an "emitDecoratorMetadata": true, option in tsconfig.json ` +
146-
`and that you have imported "reflect-metadata" on top of the main entry file in your application.` +
147-
`And make sure that you have annotated the property type correctly with: ` +
148-
`Repository, MongoRepository, TreeRepository or custom repository class type.`
149-
);
141+
throw new PropertyTypeMissingError(object, propertyName);
150142
}
151143
repositoryType = propertyType;
152144
}
@@ -156,16 +148,7 @@ export function OrmRepository(entityTypeOrConnectionName?: Function|string, para
156148
case MongoRepository:
157149
case TreeRepository:
158150
if (!entityType) {
159-
throw new Error(
160-
`Missing "entityType" parameter of "@OrmRepository" decorator ` +
161-
index !== undefined
162-
? `for a "${propertyName}" method's ${index! + 1}. parameter of ${object.constructor.name} class. `
163-
: `for a property "${propertyName}" of ${object.constructor.name} class. `
164-
+
165-
`For injecting Repository, MongoRepository or TreeRepository, ` +
166-
`you have to specify the entity type due to TS reflection limitation - ` +
167-
`"entityType" parameter can be ommited only for custom repositories.`
168-
);
151+
throw new EntityTypeMissingError(object, propertyName, index);
169152
}
170153
}
171154

src/errors/EntityTypeMissingError.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export class EntityTypeMissingError extends Error {
2+
constructor(object: Object, propertyName: string, index?: number) {
3+
super(
4+
`Missing "entityType" parameter of "@OrmRepository" decorator ` +
5+
index !== undefined
6+
? `for a "${propertyName}" method's ${index! + 1}. parameter of ${object.constructor.name} class. `
7+
: `for a property "${propertyName}" of ${object.constructor.name} class. `
8+
+
9+
`For injecting Repository, MongoRepository or TreeRepository, ` +
10+
`you have to specify the entity type due to TS reflection limitation - ` +
11+
`"entityType" parameter can be ommited only for custom repositories.`
12+
);
13+
14+
Object.setPrototypeOf(this, EntityTypeMissingError.prototype);
15+
}
16+
}

src/errors/ParamTypeMissingError.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class ParamTypeMissingError extends Error {
2+
constructor(object: Object, propertyName: string, index: number) {
3+
super(
4+
`Cannot get reflected type for a "${propertyName}" method's ${index + 1}. parameter of ${object.constructor.name} class. ` +
5+
`Make sure you have turned on an "emitDecoratorMetadata": true, option in tsconfig.json. ` +
6+
`and that you have imported "reflect-metadata" on top of the main entry file in your application.` +
7+
`And make sure that you have annotated the property type correctly with: ` +
8+
`Repository, MongoRepository, TreeRepository or custom repository class type.`
9+
);
10+
11+
Object.setPrototypeOf(this, ParamTypeMissingError.prototype);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class PropertyTypeMissingError extends Error {
2+
constructor(object: Object, propertyName: string) {
3+
super(
4+
`Cannot get reflected type for a property "${propertyName}" of ${object.constructor.name} class. ` +
5+
`Make sure you have turned on an "emitDecoratorMetadata": true, option in tsconfig.json ` +
6+
`and that you have imported "reflect-metadata" on top of the main entry file in your application.` +
7+
`And make sure that you have annotated the property type correctly with: ` +
8+
`Repository, MongoRepository, TreeRepository or custom repository class type.`
9+
);
10+
11+
Object.setPrototypeOf(this, PropertyTypeMissingError.prototype);
12+
}
13+
}

0 commit comments

Comments
 (0)