Laravel未在自定义异常中调用报表方法



尝试使用自定义异常:

namespace AppExceptionsCloud;
use Exception;
class CantConfirmOrder extends Exception
{
public function report()
{
info('test exception');
}
}

但当我把它扔进修补程序时,没有任何东西写入日志:

>>> throw new CantConfirmOrder('test');
[!] Aliasing 'CantConfirmOrder' to 'AppExceptionsCloudCantConfirmOrder' for this Tinker session.
AppExceptionsCloudCantConfirmOrder with message 'test'

Handler.php:

public function report(Throwable $exception)
{
parent::report($exception);
}

我需要用try-catch手动调用report((吗?我以为我投球时会自动调用它。

在HTTP请求的上下文中,路由将接收并调用报告方法。因此,如果您在Controller方法或其他路由操作中抛出错误,则应该调用它。你可以在这样的测试中尝试:

Route::get('x', fn() => throw new CantConfirmOrder('test));  $this->get('x');

调用该方法的Route类/方法是

https://laravel.com/api/9.x/Illuminate/Routing/Pipeline.html#method_handleException

最新更新