Whoops 2.0 + Laravel 5.2

Published 1 year ago by canfiax

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?

Best Answer (As Selected By canfiax)
Dwight

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);
    }
skliche
skliche
1 year ago (102,330 XP)

@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);
}
bashy
bashy
1 year ago (1,086,840 XP)

Do you really need the Whoops error page?

bobbybouwmann

What isn't working when you follow the tutorial?

Dwight
Dwight
1 year ago (17,260 XP)

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);
    }
canfiax

@dwightwatson just my point! Your code seems to work, and does not catch validation execptions. Great!

canfiax

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

Dwight
Dwight
1 year ago (17,260 XP)

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

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);
    }
behindyou

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 sign in or create an account to participate in this conversation.