Exception to json sends text/html header
When in the application an exception is thrown it nicely detects if it is an ajax request en sends a json string back, but the header that is send with it is Content-Type:text/html.
Should it not check Request::wantsJson() and send a response with Content-Type: application/json ?
Currently i have a workaround by setting the header in App::error but it should be default right?
wut... he is suggesting the header changed and that's it... laravel already sends json responses in this case (as he says) but doesn't send the correct header according to the car.
@robclancy is right. It already sends a json encoded string back but without the right header.
Are you referring to the whoops response?
No, he is not.
So @robclancy you are saying that works fine then? And it is just happening when debug mode is set to false?
Go away. What he is saying is clear at the very start.
@GrahamCampbell I'll explain again. When you go to example.com/non/existing/route with your browser you get a nice error page with Symfony\Component\HttpKernel\Exception\NotFoundHttpException.
When you go to the same route with an ajax request it gives you {"error":{"type":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException"}} etc.
The header send with the ajax response is Content-Type:text/html and it should be Content-Type:application/json
Thanks @marcvdm. What I was asking is is this caused by the "plain" exception handler, or the "debug" handler. That's all. https://i.imgur.com/846lAl2.png
What you were asking was irrelevant and wasting our time as the description provided was clear. You don't need to validate every issue, don't you have stuff to work on? White space to fix?
Edit: that screenshot explains so much... so much
Oh rly.
It probably should. I think it could be done in WhoopsDisplayer if we are using the JsonHandler, but you can look into it.
I'm actually making this change in my copy of Laravel for my application.
WhoopsDisplayer is a pretty good place to make the change. How would you recommend going about it?
Should I just loop through all of the handlers, and if JsonResponseHandler is present, set a Content-Type: application/json header?
I added the following to WhoopsDisplayer:
public function display(Exception $exception)
{
$status = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500;
$headers = $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : array();
// Crude workaround...
// I'm sure there's a better way to do this...
foreach($this->whoops->getHandlers() as $index => $handler) {
if (get_class($handler) === "Whoops\\Handler\\JsonResponseHandler")
$headers['Content-Type'] = "application/json";
}
return new Response($this->whoops->handleException($exception), $status, $headers);
}
Would the case ever exist where there would be more than one JsonResponseHandler? Because it would add the content-type multiple times. I'm also not sure if at any point further along the Content-Type would be overridden by anything else.