HiveBrain v1.2.0
Get Started
← Back to all entries
debugphplaravelMinor

Exception Handler on a Switch

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
switchhandlerexception

Problem

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

Solution

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);
    }

}

Code Snippets

throw new MyException("A new error summary", $e);
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);
    }

}

Context

StackExchange Code Review Q#99004, answer score: 2

Revisions (0)

No revisions yet.