简体   繁体   中英

How to access exception thrown in failed Laravel queued Job

I'm using a Laravel 5.2 Job and queuing it. When it fails it fires the failed() method on the job:

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? ...
    }
}

In the failed() method, how do I access the exception that was thrown when the Job failed?

I understand I can catch the Exception in handle() but I wanted to know if it's accessable in failed()

you can use this code :

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

but it is available from laravel 5.3. version. For older laravel versions you can use some not elegant solutions like @Capitan Hypertext suggested.

This should work

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

I'm assuming you made the failed method? If that's a thing in Laravel, this is the first I've seen of it.

you can try something like this

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

    dd($throwable);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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