Chapters
28 Chapters
|
1:45:06
|
This course is archived!
This tutorial uses a deprecated micro-framework called Silex. The fundamentals of REST are still valid, but the code we use can't be used in a real application.
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
25.
ApiProblemException and Exception Handling
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
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.
This tutorial uses a deprecated micro-framework called Silex. The fundamentals of REST are still valid, but the code we use can't be used in a real application.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"silex/silex": "~1.0", // v1.3.2
"symfony/twig-bridge": "~2.1", // v2.7.3
"symfony/security": "~2.4", // v2.7.3
"doctrine/dbal": "^2.5.4", // v2.5.4
"monolog/monolog": "~1.7.0", // 1.7.0
"symfony/validator": "~2.4", // v2.7.3
"symfony/expression-language": "~2.4" // v2.7.3
},
"require-dev": {
"behat/mink": "~1.5", // v1.5.0
"behat/mink-goutte-driver": "~1.0.9", // v1.0.9
"behat/mink-selenium2-driver": "~1.1.1", // v1.1.1
"behat/behat": "~2.5", // v2.5.5
"behat/mink-extension": "~1.2.0", // v1.2.0
"phpunit/phpunit": "~5.7.0", // 5.7.27
"guzzle/guzzle": "~3.7" // v3.9.3
}
}
11 Comments
Hi.
I think it's missing a status code in the new instance of the class ApiProblem in throwApiProblemValidationException function:
Now yes: all green!
:)
You're right! I've fixed it at https://github.com/knpunive.... Thanks!
I'm watching again this wonderful tutorial, while trying to implement the same functions in Symfony: which is the equivalent of $this->error? I thought it was an event listener for "event.exception", but the event does not contain the status code... Any suggestion?
Hi Pietrino!
You're basically right that $this->error() is equivalent to registering a listener on the "kernel.exception" even (you said, "event.exception", but I know what you meant!). But in Symfony, you are passed a "GetResponseForExceptionEvent" event object to your method. The following code will get you what you need:
Silex basically does this to get the status code for you and then calls your callback that you passed to error().
Cheers!
Thanks, it worked like a charm! :)
What was the reasoning behind creating two separate classes: ApiProblem and ApiProblemException? We could combine them and I don't think that would break principles like SRP.
I can't immediately think of a problem with this. If you try it out, let me know if you run into anything ugly :). The reason was just a natural evolution: I wanted to model the ApiProblem first... and then later, I was worrying and handling exceptions. That's why I think you might be right that combining these won't be an issue.
Cheers!
What I did now is the following: I created a directory called 'Exception' in my AppBundle and created an interface called 'Exception'. This interface has one method: buildResponse. Then I created the ApiProblemException (https://gist.github.com/thejager/e2b26505aa4d2bb0b266887541f2351b ) and made it implement that interface. It basically has the ApiProblem build into it and the buildResponse method, well..., builds the response ;).
Then my ExceptionListener (I'm using Symfony 3) looks like this:
Now if I need a new exception type, for some reason, with some crazy custom Response I can just create a new exception class and make it implement the Exception interface. No need to mess with the ExceptionListener. I think it's cool but only time will tell if it actually works well ;)
Don't worry - rescued the message :). I have no idea why Disqus occasionally marks things as spam!
And yep, I like this approach - the approach if creating an interface for common exceptions (this is especially cool in a library - where that library can have every exception it throws implement some base interface). So, I like it!
Cheers!
Cool, thanks. I will do that :)
Hey I don't know if you can have anything control over it but my last comment was "Detected as spam". :(
"Houston: no signs of life"
Start the conversation!