Laravel展示了开发环境中的Whoops调试器


Laravel show Whoops debugger in dev environment

我的业务逻辑中需要try-catch块。因此,我可以用更多的上下文来记录错误。

try {
    // business logic
} catch(Exception $e) {
    // Log message like 'Could not create user with email something@domain.com'
    $msgForUser = CustomErrorHandler::handleError($e, $data->email);
    // Display message like 'Your user was not created bla bla bla'
    return $msgForUser;
}

我意识到我可以在App::errorstart/global.php中设置自定义错误处理,但这消除了包含特定于该函数的变量和消息的机会。

问题是,现在我的try块在开发过程中捕捉到了错误。我仍然希望在开发模式下使用Whoops异常调试器。这可能吗?

在这个问题上,你有点反对流,Laravel似乎倾向于使用异常来报告实际错误(而不是像你一样使用异常来进行应用程序控制流)。

据我所知,拉拉威尔没有任何东西能让你这么做。我会把这个逻辑放在你的CustomErrorHandler::handleError方法中

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