Skip to content

Commit

Permalink
fix: execution container no support noThrow option (#60)
Browse files Browse the repository at this point in the history
* fix: execution container no support noThrow option

* chore: rename error name
  • Loading branch information
JerrysShan authored Sep 5, 2022
1 parent ff37cd3 commit a85d5a1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class LazyInjectConstructorError extends createErrorClass('LazyInjectCons
}
}

export class ScopeEscapeError extends createErrorClass('SingletonInjectExecutionError') {
export class ScopeEscapeError extends createErrorClass('ScopeEscapeError') {
constructor(
target: Constructable<unknown>,
propertyOrIndex: string | symbol | number,
Expand Down
8 changes: 7 additions & 1 deletion src/execution_container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ export default class ExecutionContainer extends Container {
this.set({ id: EXECUTION_CONTEXT_KEY, value: ctx });
}

public get<T = unknown>(id: Identifier<T>): T {
public get<T = unknown>(
id: Identifier<T>,
options: { noThrow?: boolean; defaultValue?: any } = {},
): T {
const md = this.getDefinition(id) ?? this.parent.getDefinition(id);
if (!md) {
if (options.noThrow) {
return options.defaultValue;
}
throw new NotFoundError(id);
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface ReflectMetadataType {
}

export interface ContainerType {
get<T>(id: Identifier<T>): T;
get<T>(id: Identifier<T>, options?: { noThrow?: boolean; defaultValue?: any }): T;
set(options: Partial<InjectableDefinition>): this;
getDefinition(id: Identifier): InjectableMetadata | undefined;
getInjectableByTag(tag: string): any[];
Expand Down
9 changes: 8 additions & 1 deletion test/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,21 +371,28 @@ describe('container#scopeEscape', () => {
});

describe('container#noThrow', () => {
let container;
let container, execContainer;
beforeAll(() => {
container = new Container('no_throw');
execContainer = new ExecutionContainer({}, container);
});
it('should throw error when no find definition with identifier', () => {
expect(() => {
container.get(Phone);
}).toThrowError('identifier was not found in the container');

expect(() => {
execContainer.get(Phone);
}).toThrowError('identifier was not found in the container');
});

it('should not throw error when no find definition with identifier', () => {
expect(() => {
const phone = new Phone();
const value = container.get(Phone, { noThrow: true, defaultValue: phone });
const value2 = execContainer.get(Phone, { noThrow: true, defaultValue: phone });
expect(value).toBe(phone);
expect(value2).toBe(phone);
}).not.toThrowError();
});
});

0 comments on commit a85d5a1

Please sign in to comment.