Skip to content

Commit

Permalink
better stringify key in LAZY_IN_SYNC
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-marechal committed May 26, 2023
1 parent 1133992 commit cd46957
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
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 A] 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.`);
});
});

0 comments on commit cd46957

Please sign in to comment.