Whoops in Laravel 5.1

Miss the old, pretty Whoops!?

If you are like me and really miss the old Whoops! screen, here is how to bring it back.

First you just need to composer require:

composer require "filp/whoops: ~1.0"

But wait! Why would you want the old whoops?!

It is especially useful for debugging problems with your views. You can see the error in context:

Here is a standard whoops screen:

Old whoops

Here is the old pretty whoops screen:

Error in context

See how much easier that is to fix right away?

Okay, carry on.

Update your app/Exceptions/Handler.php render method to look something like this:

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if (config('app.debug') && app()->environment() != 'testing') {
        return $this->renderExceptionWithWhoops($request, $e);
    }

    return parent::render($request, $e);
}

Now I just need to add a method called renderExceptionWithWhoops:

/**
 * Render an exception using Whoops.
 *
 * @param  \Illuminate\Http\Request $request
 * @param  \Exception $e
 * @return Response
 */
protected function renderExceptionWithWhoops($request, Exception $e)
{
    $whoops = new \Whoops\Run;

   $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());

    return new \Illuminate\Http\Response(
        $whoops->handleException($e),
        $e->getStatusCode(),
        $e->getHeaders()
    );
}

That's pretty good, but we can take it one step further, I think.

Not only do I want the old pretty whoops back, but I'm also pretty sick of getting an html whoops when I am debugging my ajax requests, so I will also use the JsonResponseHandler as well!

Check it out!

/**
 * Render an exception using Whoops.
 *
 * @param  \Illuminate\Http\Request $request
 * @param  \Exception $e
 * @return Response
 */
protected function renderExceptionWithWhoops($request, Exception $e)
{
    $whoops = new \Whoops\Run;

    if ($request->ajax()) {
        $whoops->pushHandler(new \Whoops\Handler\JsonResponseHandler());
    } else {
        $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler());
    }

    return new \Illuminate\Http\Response(
        $whoops->handleException($e),
        $e->getStatusCode(),
        $e->getHeaders()
    );
}

Now my life is good again!

Thanks to Matt Stauffer for his blog post a while back.