-
Notifications
You must be signed in to change notification settings - Fork 169
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
improve exceptions #70
Comments
here's another
https://github.com/microsoft/tsyringe/blob/master/src/dependency-container.ts#L172 I think that prefixing with |
I actually just ran into this issue as well, providing Minimal reproduction: import 'reflect-metadata';
import { container } from 'tsyringe';
container.register('foo', { useValue: null });
container.resolve('foo'); Resulting error:
This was difficult to debug, not knowing that |
Me too, this is bit of a problem. How do we handle optional dependencies? @chrislambe did you find a solution for it? |
@avin-kavish , what is the scenario you have that needs the Optional decorator? I haven't seen the need for it yet, but I do see that Angular has it, and we could follow suit if there were compelling use-cases. |
Optional dependencies, I tried to use it to inject the current Principal acting on the resource. And sometimes the request can be from an unauthenticated user. I was planning to use the null value to indicate that. I did, container.register(Principal, { useValue: req.user }) // req.user may be null But I can model the problem differently. I could change it to a wrapper class, similar to ASP.Net Core identity, class Principal {
ctor(public user: User)
isLoggedIn() {
return !!this.user
}
}
container.register(Principal, { useValue: new Principal(req.user) }) So yeah, I can't exactly say optional dependencies are needed, but I think having them wouldn't be bad. |
@avin-kavish I ended up just doing this: container.register('foo', { useFactory: instanceCachingFactory(() => possiblyNullishValue) }); And it seems to work fine, though I suspect there's probably a reason not to do this. |
If that works and the other doesn't, it's a bug. |
This issue still persists as of today. For optional dependencies, tsyringe throws |
hmmm... getting this in a jest test...
it's complaining at test
the problem was I didn't include
@inject("tag")
in my dependencies.For this ticket I'd like to see an improved exception. At the very least I need to know which class it was trying to inject when it failed, ideally with the type or parameter position trying to be injected, as I had about a dozen. At best I'd like to see the actual source of the resolve in the stacktrace.
The text was updated successfully, but these errors were encountered: