Ali Gajani
Ali Gajani

Reputation: 15091

How do I catch a specific exception thrown by the Laravel 5.6 Queue's failed method?

My handler() throws a SalesforceException. I want to watch that and log the particular error message from the it, using the failed() method on the Queue in Laravel 5.6. If I do the below, I am getting the error:

Error

[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 ...

Code

  public function failed(Exception $e)
    {
        try {

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

Upvotes: 0

Views: 647

Answers (1)

Brian Lee
Brian Lee

Reputation: 18187

Set up a listener in your AppServiceProvider to listen for failed jobs from the queue:

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
        }
     }); 
} 

More here on failed job events.

Upvotes: 1

Related Questions