繁体   English   中英

如何访问失败的 Laravel 排队作业中抛出的异常

[英]How to access exception thrown in failed Laravel queued Job

我正在使用 Laravel 5.2 Job并将其排队。 当它失败时,它会在作业上触发failed()方法:

class ConvertJob extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, DispatchesJobs;


    public function __construct()
    {

    }

    public function handle()
    {
        // ... do stuff and fail ...
    }

    public function failed()
    {
        // ... what exception was thrown? ...
    }
}

failed()方法中,如何访问Job失败时抛出的异常?

我知道我可以在handle()中捕获异常,但我想知道它是否可以在failed()中访问

您可以使用以下代码:

public function failed(Exception $exception)
{
    // Send user notification of failure, etc...
}

但可从laravel 5.3中获得。 版。 对于较旧的laravel版本,您可以使用一些不太雅致的解决方案,例如建议使用@Capitan Hypertext。

这应该工作

public function handle()
{
    // ... do stuff
    $bird = new Bird();

    try {
        $bird->is('the word');
    }
    catch(Exception $e) {
        // bird is clearly not the word
        $this->failed($e);
    }
}

public function failed($exception)
{
    $exception->getMessage();
    // etc...
}

我假设您制作了failed方法? 如果这是Laravel中的事情,这是我第一次看到它。

你可以尝试这样的事情

   /**
 * failed
 *
 * @param  mixed $throwable
 * @return void
 */
public function failed(Throwable $throwable){

    dd($throwable);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM