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?