Skip to content

Instantly share code, notes, and snippets.

@brcontainer
Last active March 27, 2019 17:58
Show Gist options
  • Save brcontainer/15557933f979cfed572734642dbf4620 to your computer and use it in GitHub Desktop.
Save brcontainer/15557933f979cfed572734642dbf4620 to your computer and use it in GitHub Desktop.

Getting start

Install Whoops (prefer version 2)

cd projectfolder
composer require filp/whoops

Or edite composer.json and set require values, like this:

    ...
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.2.*",
        "filp/whoops": "2.1.*"
    },
    ...

After run:

cd projectfolder
composer update

Customing Laravel5

Open projectfolder/app/Exceptions/Handler.php and replace function render by example in gist:

    public function render($request, Exception $e)
    {
        if ($e instanceof HttpException) {
            return parent::render($request, $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());
    }

app/Exceptions/Handler.php

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        AuthorizationException::class,
        HttpException::class,
        ModelNotFoundException::class,
        ValidationException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    public function report(Exception $e)
    {
        parent::report($e);
    }

    /**
     * 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 ($e instanceof HttpException) {
            //HTTP errors don't need Whoops
            return parent::render($request, $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());
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment