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

better stringify key in LAZY_IN_SYNC #1511

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 13 additions & 3 deletions src/constants/error_msgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ export const MISSING_INJECTABLE_ANNOTATION = 'Missing required @injectable annot
export const MISSING_INJECT_ANNOTATION = 'Missing required @inject or @multiInject annotation in:';
export const UNDEFINED_INJECT_ANNOTATION = (name: string) =>
`@inject called with undefined this could mean that the class ${name} has ` +
'a circular dependency problem. You can use a LazyServiceIdentifer to ' +
'a circular dependency problem. You can use a LazyServiceIdentifer to ' +
'overcome this limitation.';
export const CIRCULAR_DEPENDENCY = 'Circular dependency found:';
export const NOT_IMPLEMENTED = 'Sorry, this feature is not fully implemented yet.';
export const INVALID_BINDING_TYPE = 'Invalid binding type:';
export const NO_MORE_SNAPSHOTS_AVAILABLE = 'No snapshot available to restore.';
export const INVALID_MIDDLEWARE_RETURN = 'Invalid return type in middleware. Middleware must return!';
export const INVALID_FUNCTION_BINDING = 'Value provided to function binding must be a function!';
export const LAZY_IN_SYNC = (key: unknown) => `You are attempting to construct '${key}' in a synchronous way
but it has asynchronous dependencies.`;
export const LAZY_IN_SYNC = (key: unknown) => `You are attempting to construct ${keyToString(key)} in a synchronous way ` +
'but it has asynchronous dependencies.';

export const INVALID_TO_SELF_VALUE = 'The toSelf function can only be applied when a constructor is ' +
'used as service identifier';
Expand Down Expand Up @@ -53,3 +53,13 @@ export const CIRCULAR_DEPENDENCY_IN_FACTORY = (factoryType: string, serviceIdent
`service identifier '${serviceIdentifier}'.`;

export const STACK_OVERFLOW = 'Maximum call stack size exceeded';

function keyToString(key: unknown): string {
if (typeof key === 'function') {
return `[function/class ${key.name || '<anonymous>'}]`;
}
if (typeof key === 'symbol') {
return key.toString();
}
return `'${key}'`;
}
16 changes: 15 additions & 1 deletion test/constants/error_message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,18 @@ describe('ERROR_MSGS', () => {
expect(error).eql('@postConstruct error in class a: b');
});

});
it('Should properly stringify symbol in LAZY_IN_SYNC', () => {
const error = ERROR_MSGS.LAZY_IN_SYNC(Symbol('a'));
expect(error).eql(`You are attempting to construct Symbol(a) in a synchronous way but it has asynchronous dependencies.`);
});

it('Should properly stringify class in LAZY_IN_SYNC', () => {
const error = ERROR_MSGS.LAZY_IN_SYNC(class B {});
expect(error).eql(`You are attempting to construct [function/class B] in a synchronous way but it has asynchronous dependencies.`);
});

it('Should properly stringify string in LAZY_IN_SYNC', () => {
const error = ERROR_MSGS.LAZY_IN_SYNC('c');
expect(error).eql(`You are attempting to construct 'c' in a synchronous way but it has asynchronous dependencies.`);
});
});
17 changes: 6 additions & 11 deletions test/resolution/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1837,8 +1837,7 @@ describe('Resolve', () => {
container.bind<Constructable>('Constructable').to(Constructable).inSingletonScope()
.onActivation(() => Promise.resolve());

expect(() => container.get('Constructable')).to.throw(`You are attempting to construct 'Constructable' in a synchronous way
but it has asynchronous dependencies.`);
expect(() => container.get('Constructable')).to.throw(`You are attempting to construct 'Constructable' in a synchronous way but it has asynchronous dependencies.`);
});

it('Should force a class with an async post construct to use the async api', async () => {
Expand All @@ -1853,8 +1852,7 @@ describe('Resolve', () => {
const container = new Container();
container.bind<Constructable>('Constructable').to(Constructable);

expect(() => container.get('Constructable')).to.throw(`You are attempting to construct 'Constructable' in a synchronous way
but it has asynchronous dependencies.`);
expect(() => container.get('Constructable')).to.throw(`You are attempting to construct 'Constructable' in a synchronous way but it has asynchronous dependencies.`);
});

it('Should retry promise if first time failed', async () => {
Expand Down Expand Up @@ -1906,8 +1904,7 @@ describe('Resolve', () => {

container.onActivation('foo', () => Promise.resolve('baz'));

expect(() => container.get('foo')).to.throw(`You are attempting to construct 'foo' in a synchronous way
but it has asynchronous dependencies.`);
expect(() => container.get('foo')).to.throw(`You are attempting to construct 'foo' in a synchronous way but it has asynchronous dependencies.`);
});

it('Should allow onActivation (sync) of a previously binded sync object (without activation)', async () => {
Expand Down Expand Up @@ -2530,8 +2527,7 @@ describe('Resolve', () => {
container.bind<UseDate>('UseDate').to(UseDate);
container.bind<Date>('Date').toDynamicValue(() => Promise.resolve(new Date()));

expect(() => container.get<UseDate>('UseDate')).to.throw(`You are attempting to construct 'UseDate' in a synchronous way
but it has asynchronous dependencies.`);
expect(() => container.get<UseDate>('UseDate')).to.throw(`You are attempting to construct 'UseDate' in a synchronous way but it has asynchronous dependencies.`);
});

it('Should be able to resolve indirect Promise bindings', async () => {
Expand Down Expand Up @@ -2569,8 +2565,7 @@ describe('Resolve', () => {
const container = new Container();
container.bind<string>('async').toDynamicValue(() => Promise.resolve('foobar'));

expect(() => container.get<string>('async')).to.throw(`You are attempting to construct 'async' in a synchronous way
but it has asynchronous dependencies.`);
expect(() => container.get<string>('async')).to.throw(`You are attempting to construct 'async' in a synchronous way but it has asynchronous dependencies.`);
});

it('Should cache a a resolved value on singleton when possible', async () => {
Expand All @@ -2597,4 +2592,4 @@ describe('Resolve', () => {
expect(serviceFromGetAsync).eql(asyncServiceDynamicResolvedValue);
expect(serviceFromGet).eql(asyncServiceDynamicResolvedValue);
});
});
});