首页 文章

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

提问于
浏览
0

我正在使用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() 中访问

2 回答

  • 2

    这应该工作

    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 方法?如果那是's a thing in Laravel, this is the first I'已经看到了它 .

  • -1

    你可以使用这段代码:

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

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

相关问题