framework icon indicating copy to clipboard operation
framework copied to clipboard

Exception to json sends text/html header

Open marcvdm opened this issue 12 years ago • 12 comments

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?

marcvdm avatar Apr 27 '14 00:04 marcvdm

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 avatar Apr 27 '14 10:04 robclancy

@robclancy is right. It already sends a json encoded string back but without the right header.

marcvdm avatar Apr 27 '14 11:04 marcvdm

Are you referring to the whoops response?

GrahamCampbell avatar Apr 27 '14 12:04 GrahamCampbell

No, he is not.

robclancy avatar Apr 27 '14 12:04 robclancy

So @robclancy you are saying that works fine then? And it is just happening when debug mode is set to false?

GrahamCampbell avatar Apr 27 '14 12:04 GrahamCampbell

Go away. What he is saying is clear at the very start.

robclancy avatar Apr 27 '14 12:04 robclancy

@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

marcvdm avatar Apr 27 '14 12:04 marcvdm

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

GrahamCampbell avatar Apr 27 '14 12:04 GrahamCampbell

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

robclancy avatar Apr 27 '14 13:04 robclancy

Oh rly.

GrahamCampbell avatar Apr 27 '14 13:04 GrahamCampbell

It probably should. I think it could be done in WhoopsDisplayer if we are using the JsonHandler, but you can look into it.

taylorotwell avatar May 02 '14 15:05 taylorotwell

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.

nlutterman avatar Jun 23 '14 00:06 nlutterman