【问题标题】:Laravel not calling report method in custom exceptionLaravel 不在自定义异常中调用报告方法
【发布时间】:2022-12-20 22:09:02
【问题描述】:

尝试使用自定义异常:

namespace App\Exceptions\Cloud;

use Exception;

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

但是当我把它扔进修补程序时 - 没有任何内容写入日志:

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

处理程序.php:

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

我需要用 try catch 手动调用 report() 吗?我认为它会在我投掷时自动调用。

【问题讨论】:

    标签: laravel exception


    【解决方案1】:

    在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-17
      • 1970-01-01
      • 2021-07-22
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 1970-01-01
      相关资源
      最近更新 更多