12.
Handling 404's + other Errors
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.
7 Comments
Hi Ryan, I am not getting the 404 error code, everything seems to be fine but it returns a 500 code. I'm stuck :(
Failure! when making the following request:
GET: http://dev.dev/rest-symfony...
HTTP/1.1 500 Internal Server Error
Date: Mon, 04 Jan 2016 11:38:03 GMT
Server: Apache/2.4.12 (Ubuntu)
Cache-Control: no-cache, no-cache
Content-Length: 67
Connection: close
Content-Type: application/problem+json
{
"status": 500,
"type": "about:blank",
"title": "Internal Server Error"
}
Hey Daniel!
Ok, there are two causes:
A) There is some Exception being thrown somewhere that you're not aware of (so instead of the 404 NotFoundHttpException from createNotFoundException(), some other exception is being thrown)
B) You've got a bug in your event listener.
The problem is *probably* A) - but I'd put some debug code (i.e. var_dump+die) in your onKernelException() to help see what's going on. But, if (A) is the problem, here's a trick: you can look at the profiler for this request and see *what* exception happened. Try this:
1) Run the test (i.e make the request that returns a 500)
2) Go to http://dev.dev/rest-symfony...
You'll see a list of all of the recent requests - the first or second should be the 500 error. Click on the link (will be the token, like abc123) to see the profiler for that request. Then click the Exception tab to see what exception *really* happened.
I hope that helps! Making sure debugging in an API is important!
Ryan, thanks a lot for your reply!
I did that before, placed dump($e); on the onKernelRequest. Even print it to a file because linux terminal has some limitations on output size. On that file just found this:
InvalidArgumentException {#447
#message: "The HTTP status code "0" is not valid."
#code: 0
#file: "/home/daniel/dev/rest-symfony-start/app/bootstrap.php.cache"
#line: 1460
-trace: array:15 [
0 => array:3 [
"call" => "Symfony\Component\HttpFoundation\Response->setStatusCode()"
"file" => "/home/daniel/dev/rest-symfony-start/app/bootstrap.php.cache:1341"
"args" => array:1 [
0 => 0
]
]
So as we got this line: $statusCode = $e instanceof HttpExceptionInterface ? $e->getCode() : 500; and InvalidArgumentException is not an instanceof HttpExceptionInterface son statusCode is set to 500.
Am I on the right path? If so, why $event->getException does not get a 404 code?
PS: the profiler is empty
Hey Daniel!
Ah, I think I might see the problem. The line should read:
The getStatusCode() method is the key: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Exception/HttpExceptionInterface.php#L26. When you throw a 404 exception, it is this class: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Exception/NotFoundHttpException.php - and it returns 404 for this. But the getCode() (which is just a method on all Exception classes) is 0. I think that's our problem :).
Cheers!
Yes! that was the error! How could I missed? I wasn't even that difficult to spot, sometimes I get all the important things right but spend hours chasing a typo... I can keep going now with the other videos.
Thanks a lot again! Cheers!
Hi Ryan,
Just wondering,
I googled a lot but can't really find something concrete about this :
whats is the best practice for custom errors.
Do we stick with classic 4xx response code and then put the custom detail in body.
Or do we send a 200 status code (since there is no real http error) and put detail in body.
For example, here is what kind of error I want to handle :
My app required that there is at least one "Project" resource. And every time an endpoint is accessed and there is no project resource found, I have to say to the client that its a business error and that he must create one in order to process.
What's your opinion about this?
Again, thanks.
Yo Chuck!
Use 4xx! I get your point about it not being an "http" error (it's not like they sent the wrong URI or method or something), but 4xx simply means "the request (or information in the request) you sent was improper in some way". This includes validation errors, and I basically consider this to be a validation error. So yes, 4xx all the way - only 200 if the operation the user intended was successful.
And your API clients will thank you, as they will be pre-wired to know that a 4xx means trouble. For example, in jQuery, a 4xx will trigger the error callback, instead of success (and so if the user has a global AJAX error handler, it'll hit that).
Isn't it funny how hard it is to find clear answers on this stuff sometimes? Welcome to REST :)
Cheers!
"Houston: no signs of life"
Start the conversation!