php - LARAVEL 5.6: catching exception inside a job class
I am trying to catch an exception inside a job but it seems the try/catch bock has no effect.
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$progress=0;
$step=100/count($this->itemsId);
event(new JobProgressUpdate($progress, '', true, $this->broadcastChannel));
foreach ($this->itemsId as $itemId) {
$message='ITEM '.$itemId.': ';
$success=true;
try{
$this->meliCredential->setItemStatus($itemId,$this->status);
}catch(\Exception $e){
$message+=$e->getMessage();
$success=false;
}
$progress+=$step;
event(new JobProgressUpdate($progress, $message, $success, $this->broadcastChannel));
}
}
The idea is to avoid the job to be marked as failed and just send a message of the exception to the client.
so the question is, How to catch exceptions inside a job handler???
I don't want to stop the execution, that's why i need to catch the exception.
Answer
Solution:
it was just dumb question. The problem was initially namespace issue of the Exception class (initially i was not using the "\"). After that the job crashed with the line
$message+=$e->getMessage();
This was replaced for$message=$message.$e->getMessage();
and now its working fine.