如何捕获Laravel 5.6 Queue失败方法抛出的特定异常?

时间:2018-04-17 00:20:59

标签: laravel

我的handler()抛出SalesforceException。我想观察它并使用Laravel 5.6中Queue上的failed()方法记录它的特定错误消息。如果我这样做,我收到错误:

错误

[2018-04-17 00:18:12] local.ERROR: Type error: Argument 1 passed to App\Listeners\SyncNewsletterSignupToSalesforce::failed() must be an instance of Exception, instance of App\Events\NewsletterSignup given {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Type error: Argument 1 passed to App\\Listeners\\SyncNewsletterSignupToSalesforce::failed() must be an instance of Exception, instance of App\\Events\\NewsletterSignup ...

代码

  public function failed(Exception $e)
    {
        try {

        } catch(SalesforceException $e)
        {
            $response = $e->getResponse();
            $responseBodyAsString = $response->getBody()->getContents();
            logger($responseBodyAsString);
        }
    }

1 个答案:

答案 0 :(得分:1)

在AppServiceProvider中设置侦听器以侦听队列中的失败作业:

public function boot() {
     Queue::failing(function (JobFailed $event) {
        // $event->connectionName
        // $event->job 
        // $event->exception

        // check for the specific exception type
        if ($event->exception instanceof SalesforceException) {
             // do something
        }
     }); 
} 

更多信息请点击failed job events