iamab.in
iamab.in

Reputation: 2070

Laravel : How to handle Exception in View composer

In my laravel 5.5 project, view composers are used for passing data to the views.

In the view composer's constructor() a try catch block is used to catch the exceptions and a custom exception is rethrown from the catch method.

In the default exception handler of the application, custom exception is handled to display my custom error view.

Problem : The custom exception is not working properly when thrown from the view composer. Laravel's default exception error page is shown instead of my custom error page.

ProductComponentComposer.php

namespace App\Http\ViewComposers;

use Illuminate\View\View;
use App\Repositories\ProductRepository;
use Exception;
use App\Exceptions\AppCustomException;

class ProductComponentComposer
{
    protected $products;

    /**
     * Create a new product partial composer.
     *
     * @param  ProductRepository  $productRepo
     * @return void
     */
    public function __construct(ProductRepository $productRepo)
    {
        try {
            $this->products = $productRepo->getProducts();
        } catch (Exception $e) {
            throw new AppCustomException("CustomError", 1001);
        }
    }

    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with(['productsCombo' => $this->products]);
    }
}

Handler.php

public function render($request, Exception $exception)
    {
        if($exception instanceof AppCustomException) {
            //custom error page when custom exception is thrown
            return response()->view('errors.app-custom-exception', compact('exception'));
        }

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

Note : The custom exception is handled properly if thrown from the controller.

I also tried throwing the exception from the compose() method of the ProductComponentComposer instead of the __constructor(). But that also not working.

How to fix this to get my custom exception view if any exception is occured in the view composer?

Thanks in advance..

Upvotes: 0

Views: 1184

Answers (1)

gh0c
gh0c

Reputation: 1

I had the same issue where a custom exception was thrown within a method in my view composer class, yet \ErrorException is what I got displayed. There's a handler on a framework's level (\laravel\framework\src\Illuminate\View\Engines\PhpEngine.php:45) I believe is causing this.

Fix I've applied:

App\Exceptions\Handler.php

public function render($request, Exception $exception)
{
    if ($exception instanceof AppCustomException ||
        $exception instanceof \ErrorException && 
        $exception->getPrevious() instanceof AppCustomException
    ) {
        //custom error page when custom exception is thrown
        return response()->view('errors.app-custom-exception', compact('exception'));
    }
    // default
    return parent::render($request, $exception);
}

Make sure that what you're getting really is an instance of \ErrorException

Upvotes: 0

Related Questions