Search code examples
phplaravelexceptionwhoops

Laravel show Whoops debugger in dev environment


I am in need of having try-catch blocks inside my business logic. Thereby I can do some logging of the error with some more context.

try {
    // business logic
} catch(Exception $e) {
    // Log message like 'Could not create user with email [email protected]'
    $msgForUser = CustomErrorHandler::handleError($e, $data->email);

    // Display message like 'Your user was not created bla bla bla'
    return $msgForUser;
}

I realise I could setup custom error handling in App::error in start/global.php, however this removes the opportunity of including variables and messages specific to that function.

The problem is that now my try blocks catches errors in development. And I would like to still get the Whoops exception debugger in development mode. Is this possible?


Solution

  • You're fighting a bit against the flow on this one, Laravel seems biased towards using exceptions to report actual errors (vs. using exceptions for application control flow like you are).

    There's nothing (I know of) in Laravel that will allow you to do this. I'd put this logic in your CustomErrorHandler::handleError method

    class CustomerErrorHandler
    {
        //assuming `CustomerErrorHandler::handleError` is a static method
        //call.  If its a facade define an instance method in your service
        //class.
        static public function handleError($e)
        {
            if(...is environment where I want to see whoops...)
            {
                throw $e;
            }
    
            //if we're still here it's production
            //so do our logging and create a 
            //$msgForUser string
    
            //...
    
            return $msgForUser;
        }
    }