-
Notifications
You must be signed in to change notification settings - Fork 25.3k
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
if you forget @Injectable() on a factory, debugging is a torture #5266
Comments
Could you try
(The error message needs to be updated anyways) |
FYI, I hit this issue while working on the angular.io Dart "Http Client" chapter. Specifically in main.dart: void main() {
bootstrap(AppComponent, const [
// in-memory web api provider
const Provider(BrowserClient,
useFactory: HttpClientBackendServiceFactory, deps: const [])
]);
} Adding
|
We should cover this somewhere in the docs (maybe a troubleshooting doc?), but it would of course be better if there were a helpful error message early in the dev process. |
We've got TS errors covered now and Dart specific errors are obsolete. Closing this one. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
try to bind something like this:
here's the completely unhelpful error message I get:
nothing in the remaining stack trace hints at where the problem is.
to figure it out, you have to go to application_ref.dart:429, read a bunch of code to figure
out where the injector is getting set, guess that it's on line 268, set a breakpoint there,
try to run this.injector.resolveAndCreate yourself in the debugger, and extract a slightly more
meaningful stack trace - and i mean slightly. this is what i get:
type, type of type of type... what??
still no part of the stack trace tells me where the problem is, just that it involves the type Map.
<huge leap of imagination>
now i recall that i recently bound a Map, so i go there.
</huge leap of imagination>
nothing leaps out at me as being wrong.
so i search all over other people's code to see how they do it and see that
some, but not quite all, people put @Injectable() on their factories.
why are those other people who don't put @Injectable() not broken?
answer: they probably are but don't know it yet because their code isn't being exercised.
how about an error message more like this?:
"Injector could not create an instance for injector key because it could not find factory name.
Perhaps you forgot to annotate the factory with @Injectable()?"
of course, if you knew factory name, then why do i need to put an annotation on it?
obviously you found it. so maybe you can only say
"Injector could not create an instance for injector key because it could not find a corresponding factory. Perhaps you forgot to annotate the factory with @Injectable()?".
but at this point, i have to wonder how you found my binding. it has the factory inside it.
so let's suppose that you can't find my binding.
ok, then, how the heck did you find my binding just because i put @Injectable on a factory that it refers to?!
so basically, i have no idea what twisted things angular is doing here, but the error message needs improvement or better yet, get rid of the seemingly senseless requirement to add @Injectable() on every factory method. the boilerplate is bad enough due to forbidding anonymous factory methods.
The text was updated successfully, but these errors were encountered: