-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance errors and introduce providedIn
Signed-off-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
- Loading branch information
Showing
12 changed files
with
175 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ServiceIdentifier } from '../di/types'; | ||
import { getServiceIdentifierName } from '../utils'; | ||
|
||
export class ProviderAlreadyDefinedError<T> extends Error { | ||
constructor(private _moduleName: string, private _serviceIdentifier: ServiceIdentifier<T>) { | ||
super(` | ||
GraphQL-Modules Error: Provider has been already defined! | ||
- #Module #${_moduleName} already has a #Provider #${getServiceIdentifierName(_serviceIdentifier)}. | ||
Possible solutions: | ||
- Provider must have 'override: true' field. | ||
`); | ||
Object.setPrototypeOf(this, ProviderAlreadyDefinedError.prototype); | ||
Error.captureStackTrace(this, ProviderAlreadyDefinedError); | ||
} | ||
get moduleName() { | ||
return this._moduleName; | ||
} | ||
|
||
get serviceIdentifier(): ServiceIdentifier<T> { | ||
return this._serviceIdentifier; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ServiceIdentifier } from '../di/types'; | ||
import { getServiceIdentifierName } from '../utils'; | ||
|
||
export class ProviderNotValidError<T> extends Error { | ||
constructor(private _moduleName: string, private _serviceIdentifier: ServiceIdentifier<T>) { | ||
super(` | ||
GraphQL-Modules Error: Provider is not valid! | ||
- #Module #${_moduleName} provides an invalid #Provider #${getServiceIdentifierName(_serviceIdentifier)}! | ||
Possible solutions: | ||
- Provider must be a class itself, | ||
or provides a valid identifier with 'useValue', 'useFactory' or 'useClass'. | ||
`); | ||
Object.setPrototypeOf(this, ProviderNotValidError.prototype); | ||
Error.captureStackTrace(this, ProviderNotValidError); | ||
} | ||
get moduleName() { | ||
return this._moduleName; | ||
} | ||
|
||
get serviceIdentifier(): ServiceIdentifier<T> { | ||
return this._serviceIdentifier; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
import { ServiceIdentifier, Provider, Type, ValueProvider, ClassProvider, FactoryProvider } from './di/types'; | ||
import { ServiceIdentifier, Provider, Type, ValueProvider, ClassProvider, FactoryProvider, TypeProvider } from './di/types'; | ||
|
||
export const DESIGN_PARAM_TYPES = 'design:paramtypes'; | ||
export const INJECTABLE_OPTIONS = 'injectable:options'; | ||
|
||
export function getServiceIdentifierName<T>(serviceIdentifier: ServiceIdentifier<T>) { | ||
return serviceIdentifier['name'] || serviceIdentifier.toString(); | ||
if (typeof serviceIdentifier === 'function' && isType<T>(serviceIdentifier)) { | ||
return serviceIdentifier.name; | ||
} else { | ||
return serviceIdentifier.toString(); | ||
} | ||
} | ||
|
||
export function isType<T>(v: Provider<T>): v is Type<T> { | ||
return typeof v === 'function'; | ||
export function isType<T>(v: any): v is Type<T> { | ||
return typeof v === 'function' && 'prototype' in v; | ||
} | ||
|
||
export function isValue<T>(v: Provider<T>): v is ValueProvider<T> { | ||
export function isTypeProvider<T>(v: Provider<T>): v is TypeProvider<T> { | ||
return isType<T>(v); | ||
} | ||
|
||
export function isValueProvider<T>(v: Provider<T>): v is ValueProvider<T> { | ||
return 'useValue' in v; | ||
} | ||
|
||
export function isClass<T>(v: Provider<T>): v is ClassProvider<T> { | ||
return 'useClass' in v; | ||
export function isClassProvider<T>(v: Provider<T>): v is ClassProvider<T> { | ||
return 'useClass' in v && isType(v.useClass); | ||
} | ||
|
||
export function isFactory<T>(v: Provider<T>): v is FactoryProvider<T> { | ||
return 'useFactory' in v; | ||
export function isFactoryProvider<T>(v: Provider<T>): v is FactoryProvider<T> { | ||
return 'useFactory' in v && typeof v.useFactory === 'function'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters