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()
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With