11.
Global RESTful Exception Handling
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.
8 Comments
YAGT! Yet Another Great Tutorial.
So I am using these concepts in Symfony 4, and one of the great new features is the automatic lookup for an entity when you type hint the controller method with the Entity Class like this:
The problem is that the @ParamConverter catches the error first. That is not so bad, but I wanted the message to better than "App\Entity\VEConfig object not found by the @ParamConverter annotation."
Something like "Your VE Configuration could not be found. VEConfigUID = %s", $VEConfigUID
The only thing I could think of was to hack the apiExceptionSubscriber, and intercept the message, but that was not clean. Any Ideas?
Hi Skylar after 2 years is it still unable to custom the exception thrown by ParamConverter(NotFoundHttpException) in a clean way?
Hey Mouad E.
I guess it's not a problem that I'm not Ryan))) But I'll try to answer your question! Unfortunately there is still no way to modify default exception, so Ryan's answer is still relevant, you can make your argument optional, and then check it and throw exception manually. And I think there is a hard way))) create your own ParamConverter if you need it to be more reusable.
Cheers!
Hey Skylar!
Hmm, yes, indeed! The ParamConverter only gives you that generic message... which is normally fine (in an HTML website where you don't show any error)... but I see what you're trying to do! And yea... there's no clean way to do this: the exception thrown by ParamConverter is a generic NotFoundHttpException. So, apart from parsing the string of the message, there's no way for you to cleanly "catch" that the 404 is caused by the ParamConverter. The only solution I can think of is to make your argument optional
VEConfig $VEConfig = null. I believe this will cause the ParamConverter to NOT 404, and then your if statement will work as you expect.I hope that idea helps :).
Cheers!
the amount of time Ive revisited this course is amazing.
[Possibly bad idea] You have so much really cool stuff knocking about for testing API's, I wonder if its worth making a KNP ApiTestingBundle? I find myself copying this stuff around so much into side projects.. I bet others do too.
Yea, I've thought about that too - I'll add it to my open source list - I think it might be nice (and would let the community add more ideas to it).
Cheers!
I for one would be happy to contribute as much as I could.
"message will self destruct"
"Houston: no signs of life"
Start the conversation!