crunz icon indicating copy to clipboard operation
crunz copied to clipboard

Introduce way to handle exceptions in closures

Open PrinsFrank opened this issue 6 months ago • 0 comments

There is currently not a way to nicely handle all exceptions in closures:

$schedule->run(fn => throw new Exception())

The closure will be serialized and run in a seperate process, so on_error or set_exception_handler won't work. Even setting an exception handler in the closure itself won't work, as the exception is actually caught higher up in the console command, meaning the exception_handler won't be called.

Instead, to handle these exceptions, all closures currently can be wrapped in a try-catch:

$schedule->run(
    static function () {
        try {
            throw new Exception();
        catch (Throwable $e) {
            // log error to somewhere
        }
    }
)

This is the way we are currently handling these, but it would be nice to be able to serialize an exceptionHandler together with the closure. Would you be open to something like that?

PrinsFrank avatar Aug 20 '24 10:08 PrinsFrank