In my Laravel 5.6 application, using throw $e
seems to output the exception direct to screen and bypasses the Handler.php the exception handler. This means in development the exception is not shown by whoops and in production the exception is not reported.
As I write this, I realise I may have been using try/catches incorrectly but maybe someone can confirm this?
My current code:
try{
$stockLocation = StockLocation::findOrFail($request->location_id);
} catch (\Exception $e){
if($e instanceof ModelNotFoundException){
throw new UserNotifyException('This stock location could not be found on the system', 404);
}
throw $e;
}
My intention here was to re-throw any exception that is not a "model not found" and I expected it to be handled in exactly the same way as if the try catch was not there at all?
I now realise I should have probably written the above as:
try{
$stockLocation = StockLocation::findOrFail($request->location_id);
} catch (ModelNotFoundException $e){
throw new UserNotifyException('This stock location could not be found on the system', 404);
}
Additional to this, is there a better way to handle exceptions that I do not which to be logged? Currently I transform the exception into my own custom exception class (UserNotifyException) and then in Handler.php I ensure this is not reported.