09.
ApiProblemException
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
10 Comments
Hi everyone,
At the end of the video I have this error on my code
(1/1) ServiceCircularReferenceException
Circular reference detected for service "AppBundle\Api\ApiProblemException", path: "AppBundle\Api\ApiProblemException -> AppBundle\Api\ApiProblemException"..
I tried few things, it seems it has something to do with the $statusCode from the ApiProblemException.
Any idea?
Thanks,
Julien
Hey julien moulis
This is strange, I believe this error happens when a service (lets call it A) requires service B, but service B requires service A aswell.
Can you tell me which version of Symfony are you using?
Try clearing the cache or running "composer update", if that doesn't fix it, show me the full error message and if possible your services.yml
Cheers!
Hey MolloKhan Thanks for reply.
I'm using Symfony 3.3.13.
Actually if you add $statusCode as attributes in the ApiProblemException constructor, there is no circular error.
Here is my service.yml (it's the standard one), nothing fancy.
Here is the full error:
(1/1) ServiceCircularReferenceException
Circular reference detected for service "AppBundle\Api\ApiProblemException", path: "AppBundle\Api\ApiProblemException -> AppBundle\Api\ApiProblemException".
in CheckCircularReferencesPass.php (line 67)
at CheckCircularReferencesPass->checkOutEdges(array(object(ServiceReferenceGraphEdge), object(ServiceReferenceGraphEdge)))
in CheckCircularReferencesPass.php (line 43)
at CheckCircularReferencesPass->process(object(ContainerBuilder))
in Compiler.php (line 141)
at Compiler->compile(object(ContainerBuilder))
in ContainerBuilder.php (line 731)
at ContainerBuilder->compile()
in Kernel.php (line 573)
at Kernel->initializeContainer()
in Kernel.php (line 117)
at Kernel->boot()
in Kernel.php (line 166)
at Kernel->handle(object(Request))
in app_dev.php (line 29)
at require('/var/www/html/webagence/web/app_dev.php')
in router.php (line 42)
Yo julien moulis!
Ok, I have a theory :). Your ApiProblemException constructor should look like this:
Is it possible that your's looks like this by mistake?
Let me know! If my theory is wrong, then I want to look further :).
Cheers!
Hello @weaverryan,
Well... I guess it's probably because I mixed up different version of the code. Because, nos it works... Without doin' anything except going further in the tutos... Sorry
Thanks
Hey julien moulis!
Well dang :). But, you piqued my curiosity for sure! And I AM able to replicate this :). So, let's talk about a few things:
A) First, the fix: any workaround that doesn't feel like a hack is fine :). This will make sense when I explain more :). Technically the best fix would be to add one more excludes to your service auto-registration:
B) So... what the heck is going on? In Symfony 3.3 and lower, there is a bunch of extra magic in autowiring. And actually, it's the \Exception argument in ApiProblemException (+ some other bad luck) which causes the crazy error. In Symfony 4, this extra magic is completely removed. In other words, this is kind of a Symfony bug, and it's gone in Symfony 4. In Symfony 3.4, you can opt into the proper, 4.0 behavior by adding this flag: https://github.com/symfony/symfony-standard/blob/3.4/app/AppKernel.php#L54
If you want to nerd-out further, here's the full explanation:
1) All your classes in src/ are loaded as services. Some don't need to be services (like ApiProblemExeption), but that's ok - as long as you never try to use them as services, they're discarded.
2) But, Symfony still tries to figure out how to autowiring ApiProblemException. When it sees the \Exception type-hint, in Symfony 3.3 (this is the bad behavior), it looks through ALL the services on the container to see if any extend \Exception. And guess what!? Exactly one does: ApiProblemException itself! So, it trie to autowire itself, into itself :).
I hope that helps!
Ok it’s clear. What’s terrible, it’s that I followed the 3.3 services tutorial... Well I guess I got to back on it...
Hi,
+1 for this explanation and solution.
This happened to me, as well. Since I do not have sufficient time to watch your new S4 courses, I'm stucked for the time being at Symfony 3.3 :).
I also received the following error message.
When I write
instead of
by missing out the ApiProblem typehint in the constructor of ApiProblemException.php, the error is gone :-)
But I suppose your solution is a bit cleaner.
I would not recommend removing the type-hint, that's how Symfony detects what to inject into your services.
Cheers!
Ok I added ApiProblemExceptionClass to service.yml
And it seems to work
"Houston: no signs of life"
Start the conversation!