Search code examples
laravellaravel-5.3

Intercepting Laravel job failing event to change log written


I can catch the event with \Queue::failing() but how do I prevent the Laravel log from being written when a specific failure reason is detected?

For instance, when a job failed because it was attempted too many times, a MaxAttemptsExceededException is fired during failing. Suppose I wanted to interrupt that to add something to the log message or even change the log type to "warning" instead of "error". How would I do that?


Solution

  • You can catch exceptions by class within the Exception Handler Class.

    Using the report method:

    public function report(Exception $exception)
    {
        if ($exception instanceof MaxAttemptsExceededException) {
            // add custom message
            // optionally return parent::report($exception); to continue normal flow
        }
    
        return parent::report($exception);
    }