laravellaravel-queue

Laravel - Redis error when dispatching a job


On one of the systems that I take care of, some times some jobs don't get dispatched due to a connection problem with Redis and this ends up returning an error to the user, on our side we can ignore this error and just miss this job, I looked for how to deal with it on Google and I didn't find anything about it.

public function sendMessage(Request $request, Model $model)
{
    // Do the necessary stuff
    
    ResolveMessageBilling::dispatch($model, $request->all());

    return response()->json([
        'message' => 'The message was succesfully sent'
    ], 200);
}

This is the error we are getting: RedisException - socket error on read socket

How to ignore the error if it occurs? A simple try/catch can resolve the issue?

public function sendMessage(Request $request, Model $model)
{
    // Do the necessary stuff

    try {
        ResolveMessageBilling::dispatch($model, $request->all());
    } catch(\Exception $e) {}

    return response()->json([
        'message' => 'The message was succesfully sent'
    ], 200);
}

Solution

  • If you want to bypass ANY error, you should use \Throwable instead of \Exception

    public function sendMessage(Request $request, Model $model)
    {
        // Do the necessary stuff
    
        try {
            ResolveMessageBilling::dispatch($model, $request->all());
        } catch(\Throwable $e) {}
    
        return response()->json([
            'message' => 'The message was succesfully sent'
        ], 200);
    }
    

    see Error Hierarchy: https://www.php.net/manual/en/language.errors.php7.php

    If you want to bypass only the \RedisException, you should be able to use:

    public function sendMessage(Request $request, Model $model)
    {
        // Do the necessary stuff
    
        try {
            ResolveMessageBilling::dispatch($model, $request->all());
        } catch(\RedisException $e) {}
    
        return response()->json([
            'message' => 'The message was succesfully sent'
        ], 200);
    }