Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: create custom errors for ContainerRegistry #760

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/container-registry.class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ContainerInstance } from './container-instance.class';
import { ContainerIdentifier } from './types/container-identifier.type';
import { ContainerNotFoundError } from './error/container-not-found.error';
import { CannotRegisterContainerError } from './error/cannot-register-container.error';

/**
* The container registry is responsible for holding the default and every
Expand Down Expand Up @@ -33,19 +35,16 @@ export class ContainerRegistry {
*/
public static registerContainer(container: ContainerInstance): void {
if (container instanceof ContainerInstance === false) {
// TODO: Create custom error for this.
throw new Error('Only ContainerInstance instances can be registered.');
throw new CannotRegisterContainerError('Only ContainerInstance instances can be registered.');
}

/** If we already set the default container (in index) then no-one else can register a default. */
if (!!ContainerRegistry.defaultContainer && container.id === 'default') {
// TODO: Create custom error for this.
throw new Error('You cannot register a container with the "default" ID.');
throw new CannotRegisterContainerError('You cannot register a container with the "default" ID.');
}

if (ContainerRegistry.containerMap.has(container.id)) {
// TODO: Create custom error for this.
throw new Error('Cannot register container with same ID.');
throw new CannotRegisterContainerError('Cannot register container with same ID.');
}

ContainerRegistry.containerMap.set(container.id, container);
Expand All @@ -70,8 +69,7 @@ export class ContainerRegistry {
const registeredContainer = this.containerMap.get(id);

if (registeredContainer === undefined) {
// TODO: Create custom error for this.
throw new Error('No container is registered with the given ID.');
throw new ContainerNotFoundError(id);
}

return registeredContainer;
Expand All @@ -91,8 +89,7 @@ export class ContainerRegistry {
const registeredContainer = ContainerRegistry.containerMap.get(container.id);

if (registeredContainer === undefined) {
// TODO: Create custom error for this.
throw new Error('No container is registered with the given ID.');
throw new ContainerNotFoundError(container.id);
}

/** We remove the container first. */
Expand Down
14 changes: 14 additions & 0 deletions src/error/cannot-register-container.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Thrown when ContainerRegistry cannot register the given container.
*/
export class CannotRegisterContainerError extends Error {
public name = 'CannotRegisterContainerError';

get message(): string {
return this._message;
}

constructor(private _message: string) {
super();
}
}
28 changes: 28 additions & 0 deletions src/error/container-not-found.error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ContainerIdentifier } from '../types/container-identifier.type';

/**
* Thrown when requested container was not found.
*/
export class ContainerNotFoundError extends Error {
public name = 'ContainerNotFoundError';

/** Normalized identifier name used in the error message. */
private normalizedIdentifier: string = '<UNKNOWN_IDENTIFIER>';

get message(): string {
return (
`Container with "${this.normalizedIdentifier}" identifier was not found in the container registry. ` +
`Register it before usage via explicitly calling the "ContainerRegistry.registerContainer" function.`
);
}

constructor(identifier: ContainerIdentifier) {
super();

if (typeof identifier === 'string') {
this.normalizedIdentifier = identifier;
} else if (typeof identifier === 'symbol') {
this.normalizedIdentifier = identifier.toString();
}
}
}