Creating a custom exception class and custom handl

2019-08-01 15:17发布

Before I get to the code, let me explain my aim. My web app displays vehicles for sale. I have a need for a custom 404 page that will display 12 of the latest vehicles added to the database if the user tries to access a page that doesn't exist.

I have the following...

App\Exceptions\CustomException.php

<?php

namespace App\Exceptions;

use Exception;

class CustomException extends Exception
{
    public function __construct()
    {
        parent::__construct();
    }
}

App\Exceptions\CustomHandler.php

<?php
namespace App\Exceptions;

use Exception;
use App\Exceptions\Handler as ExceptionHandler;
use Illuminate\Contracts\Container\Container;
use App\Project\Frontend\Repo\Vehicle\EloquentVehicle;
use Illuminate\Foundation\Exceptions\Handler;
use Illuminate\Support\Facades\View;

class CustomHandler extends ExceptionHandler
{
    protected $vehicle;

    public function __construct(Container $container, EloquentVehicle $vehicle)
    {
        parent::__construct($container);

        $this->vehicle = $vehicle;
    }

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

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        $exception = Handler::prepareException($exception);

        if($exception instanceof CustomException) {
            return $this->showCustomErrorPage();
        }

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

    public function showCustomErrorPage()
    {
        $recentlyAdded = $this->vehicle->fetchLatestVehicles(0, 12);

        return View::make('errors.404Custom')->with('recentlyAdded', $recentlyAdded);
    }
}

To test this I added

throw new CustomException();

to my controller but it doesn't bring up the 404Custom view. What do I need to do to get this working?

UPDATE: Just a note for anyone who's bound their class to their model. You'll get a BindingResolutionException if you try to access a function in your class using:

app(MyClass::class)->functionNameGoesHere();

To get around this simply create a variable in the same way you would bind your class to the Container in your service provider.

My code looks like this:

protected function showCustomErrorPage()
{
    $eloquentVehicle = new EloquentVehicle(new Vehicle(), new Dealer());
    $recentlyAdded = $eloquentVehicle->fetchLatestVehicles(0, 12);

    return view()->make('errors.404Custom')->with('recentlyAdded', $recentlyAdded);
}

Amit's version

protected function showCustomErrorPage()
{
    $recentlyAdded = app(EloquentVehicle::class)->fetchLatestVehicles(0, 12);

    return view()->make('errors.404Custom')->with('recentlyAdded', $recentlyAdded);
}

1条回答
We Are One
2楼-- · 2019-08-01 16:05

Laravel calls the render function of App\Exceptions\Handler class. So overriding it will not work.

You have to add it in App\Exceptions\Handler class only.

For example:

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\AuthenticationException;
use App\Project\Frontend\Repo\Vehicle\EloquentVehicle;
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 = [
        \Illuminate\Auth\AuthenticationException::class,
        \Illuminate\Auth\Access\AuthorizationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Session\TokenMismatchException::class,
        \Illuminate\Validation\ValidationException::class,
    ];

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

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if($exception instanceof CustomException) {
            return $this->showCustomErrorPage();
        }

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

    protected function showCustomErrorPage()
    {
        $recentlyAdded = app(EloquentVehicle::class)->fetchLatestVehicles(0, 12);

        return view()->make('errors.404Custom')->with('recentlyAdded', $recentlyAdded);
    }
}
查看更多
登录 后发表回答