cloud4bpm's avatar

How do you handle your custom exceptions in Laravel 5.0/5.1?

Hi,

When we need to throw custom exceptions and catch them, what is the best place to put them?

I saw the app\exceptions folder with the handler.php class with the report and render methods. Is this the equivalent place of global.php file in previous version of Laravel?

How do you make the handle of your custom exceptions in Laravel 5.0/5.1?

Thanks.

0 likes
4 replies
phildawson's avatar

Yeah the report/render method in Handler.php should be used and test using instanceof the exception you want to catch.

As a basic example using routes.php

<?php
class FooException extends \Exception {}
throw new FooException;
public function report(Exception $e)
{
    if ($e instanceof \FooException) {
        // handle exception here 
    }

    return parent::report($e);
}

or provide a different response, like a foo error page like so

public function render($request, Exception $e)
{
    if ($e instanceof \FooException) {
        return response()->view('foo', [], 500);
    }

    return parent::render($request, $e);
}
1 like
cloud4bpm's avatar

Many thanks @phildawson,

What type of code should be present in the report and what would be the difference between handle this error or generate a custom response? Why could not send the response in the report method?

Thanks again for your answer.

phildawson's avatar
Level 26

No problem @cloud4bpm The response needs to be in the render method as that returns a response whilst the report should just be used for logging, sending an email, writing the error to file/database etc. You could theoretically do the logging in render but it makes sense to separate the two tasks. A method should only always really do one thing ideally.

Essentially when a request is handled errors thrown are caught and processed like so, as you can see providing a return value for report() would do nothing.

        } catch (Exception $e) {
            $this->reportException($e);

            $response = $this->renderException($request, $e);
        }
    ...

        return $response;
    }

both of those methods are in turn calling both those render/report methods with Illuminate\Contracts\Debug\ExceptionHandler being bound to App\Exceptions\Handler in the bootstrap

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);
    protected function reportException(Exception $e)
    {
        $this->app['Illuminate\Contracts\Debug\ExceptionHandler']->report($e);
    }
    protected function renderException($request, Exception $e)
    {
        return $this->app['Illuminate\Contracts\Debug\ExceptionHandler']->render($request, $e);
    }

it's alway a good idea to see the docblocks for the return value it's expecting.

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @param  \Exception  $e
     * @return void
     */
    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $e
     * @return \Illuminate\Http\Response
     */
2 likes

Please or to participate in this conversation.