Graham Kennedy
Graham Kennedy

Reputation: 736

Exception Not Rendering View in Laravel

Summary of Issue

I am unable to get Laravel to render the 500 default error page (views/error/500.php) using Response::error('500').

Code to Reproduce (simplified for this example)

Create a dummy controller and add the following public method:

public function action_index()
{
    try
    {
        throw new Exception;    
    }
    catch(Exception $e)
    {
        Response::error('500');
    }
}

When you run this route, notice that you will receive a blank white page and the header status is 200, not 500.

Expected Outcome

I expect the default 500 error page in views/error/500.php to be rendered and execution of the rest of the script (assuming a longer script) to be stopped.

Actual Outcome

A blank white page with the header status 200 is sent to the browser.

My Environment

More Information

Is there anything else I can provide to make this issue a little easier to understand or troubleshoot. A similar (identical?) issue came up on the Laravel forums (http://forums.laravel.io/viewtopic.php?id=2191), and it was stated as fixed a while back.

In application/routes.php I have the following code for the 500 error handler:

Event::listen('500', function()
{
    return Response::error('500');
});

You can see the Response object's source code on GitHub at https://github.com/laravel/laravel/blob/master/laravel/response.php.

Upvotes: 1

Views: 2556

Answers (2)

William Cahill-Manley
William Cahill-Manley

Reputation: 2405

You could also return the event, this would give you the same thing right now but allow you to alter behavior in a single place down the line by changing the Event registered.

public function action_index()
{
    try
    {
        throw new Exception;    
    }
    catch(Exception $e)
    {
        return Event::first('500');
    }
}

Upvotes: 2

Laurence
Laurence

Reputation: 60068

Your missing the "return". You must always "return".

public function action_index()
{
    try
    {
        throw new Exception;    
    }
    catch(Exception $e)
    {
        return Response::error('500');
    }
}

Upvotes: 6

Related Questions