Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

simplenotezy's avatar

Whoops 2.0 + Laravel 5.2

I've previously had Whoops in 5.1 and 5.0; but since 5.2 the implementation I used earlier no longer works.

I have been unable to find a way to implement Whoops 2.0 to Laravel 5.2 as is.

Used to follow this guide: https://mattstauffer.co/blog/bringing-whoops-back-to-laravel-5 - but it is not compatible with L5.2.

I know I can implement https://github.com/GrahamCampbell/Laravel-Exceptions but that includes other features that I don't want.

Any suggestions?

0 likes
9 replies
skliche's avatar

@canfiax The following works for me:

composer require filp/whoops --dev

Handler.php

public function render($request, Exception $e) {
    if (config('app.debug') && ! $request->ajax()) {
        $whoops = new \Whoops\Run;
        $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);

        return $whoops->handleException($e);
    }

    return parent::render($request, $e);
}
4 likes
bashy's avatar

Do you really need the Whoops error page?

Dwight's avatar
Dwight
Best Answer
Level 7

Instantiating Whoops in the render method doesn't work quite like it did in 5.1. The render method in Laravel's exception handler actually handles a number of default exceptions for you, like the validation HTTP exception or the ModelNotFoundException. In these instances you might actually want the page to render with the validation errors or the 404, so it's better to let them be handled that way. Instead, you can override the method in app/Exceptions/Handler.php that generates the Symfony exception response and instead use Whoops.

    /**
     * Create a Symfony response for the given exception.
     *
     * @param  \Exception  $e
     * @return mixed
     */
    protected function convertExceptionToResponse(Exception $e)
    {
        if (config('app.debug')) {
            $whoops = new \Whoops\Run;
            $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);

            return response()->make(
                $whoops->handleException($e),
                method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500,
                method_exists($e, 'getHeaders') ? $e->getHeaders() : []
            );
        }

        return parent::convertExceptionToResponse($e);
    }
5 likes
simplenotezy's avatar

@dwightwatson One question regarding your code; how do I throw the views.error.500 view if application is not in debug mode?

Dwight's avatar

That is handled by a different part of the exception handler, and should be done before a Whoops page is built. If a HttpException with a status code of 500 is thrown, and the errors.500 view exists then it should be presented. If the view does not exist, the exception will be passed on to the method above to handle it.

meigwilym's avatar

I've expanded on dwightwatson's solution to also handle ajax requests (or more specifically, requests that expect a JSON reponse):

protected function convertExceptionToResponse(Exception $e)
    {
        if (config('app.debug')) {
            $whoops = new \Whoops\Run;
            if(request()->wantsJson())
            {
                $whoops->pushHandler(new \Whoops\Handler\JsonResponseHandler());
            }
            else
            {
                $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
            }

            return response()->make(
                $whoops->handleException($e),
                method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 500,
                method_exists($e, 'getHeaders') ? $e->getHeaders() : []
            );
        }

        return parent::convertExceptionToResponse($e);
    }
4 likes
mbagentur's avatar

Hey there, I used this code in Laravel 5.3. I've got the Problem, that Validation Errors still get rendered with whoops instead of redirecting with errors! Any ideas how to solve this?

Please or to participate in this conversation.