4
\$\begingroup\$

I am working on rewriting an application in Laravel 5.1. I am new to the exception handling technique introduced in 5.0. I have overlooked taking advantage of throwing/catching exceptions frequently in the past, but am working on this now. I am most likely not up to speed on some of the best practices.

public function render($request, Exception $e)
{

    switch (true) {

        case $e instanceof Exceptions\Auctions\AuctionTypeException:
            // Should I throw a new exception here? Is passing the old exception message to a new exception good practice?
            throw new Exceptions\Notifications\AlertException($e->getMessage());
            break;

        case $e instanceof Exceptions\Auctions\AuctionArgumentException:
            // Should I throw a new exception here? Is passing the old exception message to a new exception good practice?
            throw new Exceptions\Notifications\AlertException($e->getMessage());
            break;

        ////////////////////////////////////////////
        // Custom errors to display in error view //
        ////////////////////////////////////////////
        case $e instanceof Exceptions\Notifications\AlertException:

            return response()->view('errors.notification', ['message' => $e->getMessage(), 500]);
            break;

        //////////////////////////////////////////////
        // Unknown exceptions are rendered normally //
        //////////////////////////////////////////////
        default:
            return parent::render($request, $e);
    }

}

Docs for the exception handler are here.

While most of my exceptions will fall under the "explicitly ignore and continue" umbrella, I feel like I will need to handle my exceptions in a more abstract/polymophic way later down the road, so I am preparing now. This is a huge app, and I will have several custom Exceptions.

This code currently works and behaves as excepted. Exceptions that I explicitly state I want to do something special with can be added as a case, and if it is new, or I just want to handle it normally...it falls under the default.

I would like to know if I am going down the right path - to me, this seems like a fairly clever way to handle this. Is it a good idea to catch an exception, only to throw another exception with the previous exceptions message? Are there any pitfalls to checking the instance and evaluating true like this?

\$\endgroup\$
2
\$\begingroup\$

There is nothing wrong with rethrowing an exception of a different type, the problem lies with how you are doing it. According to the PHP docs, you can pass a previous exception as a second argument to the new exception. This preserves the original stack trace. I would also provide a new error message as well:

throw new MyException("A new error summary", $e);

A new take on your switch statement:

public function render($request, Exception $e)
{

    switch (true) {

        case $e instanceof Exceptions\Auctions\AuctionTypeException:
            throw new Exceptions\Notifications\AlertException("An auction type failed to save", $e);
            break;

        case $e instanceof Exceptions\Auctions\AuctionArgumentException:
            throw new Exceptions\Notifications\AlertException("An auction argument was not valid", $e);
            break;

        ////////////////////////////////////////////
        // Custom errors to display in error view //
        ////////////////////////////////////////////
        case $e instanceof Exceptions\Notifications\AlertException:

            return response()->view('errors.notification', ['message' => $e->getMessage(), 500]);
            break;

        //////////////////////////////////////////////
        // Unknown exceptions are rendered normally //
        //////////////////////////////////////////////
        default:
            return parent::render($request, $e);
    }

}
\$\endgroup\$
  • \$\begingroup\$ So in order to access (both I guess) the message from the first exception thrown I would use $e->getPrevious() correct? Thanks for your help. \$\endgroup\$ – tomfordweb Aug 4 '15 at 15:40
  • \$\begingroup\$ Correct. I also imagine the previous error message and stack trace are generated along with the current message and stack trace by simply converting the exception object to a string. \$\endgroup\$ – Greg Burghardt Aug 4 '15 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.