php - LARAVEL 5.6: catching exception inside a job class

841

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.

39

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.

People are also looking for solutions to the problem: php - How to stop PhpStorm from indexing my `excluded` directory?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.