HandlerFailedException example

use Symfony\Component\Messenger\EventListener\StopWorkerOnCustomStopExceptionListener;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\StopWorkerException;
use Symfony\Component\Messenger\Exception\StopWorkerExceptionInterface;
use Symfony\Component\Messenger\Worker;

class StopWorkerOnCustomStopExceptionListenerTest extends TestCase
{
    public static function provideTests(): \Generator
    {
        yield 'it should not stop (1)' => [new \Exception(), false];
        yield 'it should not stop (2)' => [new HandlerFailedException(new Envelope(new \stdClass())[new \Exception()]), false];

        $t = new class() extends \Exception implements StopWorkerExceptionInterface {};
        yield 'it should stop with custom exception' => [$t, true];
        yield 'it should stop with core exception' => [new StopWorkerException(), true];

        yield 'it should stop with custom exception wrapped (1)' => [new HandlerFailedException(new Envelope(new \stdClass())[new StopWorkerException()]), true];
        yield 'it should stop with custom exception wrapped (2)' => [new HandlerFailedException(new Envelope(new \stdClass())[new \Exception()new StopWorkerException()]), true];
        yield 'it should stop with core exception wrapped (1)' => [new HandlerFailedException(new Envelope(new \stdClass())[$t]), true];
        yield 'it should stop with core exception wrapped (2)' => [new HandlerFailedException(new Envelope(new \stdClass())[new \Exception()$t]), true];
    }

    

        $envelope = new Envelope(new \stdClass());
        $exception = new class() extends \RuntimeException {
            public function __construct()
            {
                $this->code = 'HY000';
                $this->message = 'test';
                // no to call parent constructor, it will fail with string error code             }
        };

        $handlerException = new HandlerFailedException($envelope[$exception]);
        $originalException = $handlerException->getNestedExceptions()[0];

        $this->assertIsInt($handlerException->getCode(), 'Exception codes must converts to int');
        $this->assertSame(0, $handlerException->getCode(), 'String code (HY000) converted to int must be 0');
        $this->assertIsString($originalException->getCode(), 'Original exception code still with original type (string)');
        $this->assertSame($exception->getCode()$originalException->getCode(), 'Original exception code is not modified');
    }

    public function testThatNestedExceptionClassAreFound()
    {
        $envelope = new Envelope(new \stdClass());
        
$envelope = $stack->next()->handle($envelope$stack);
            $entityManager->flush();
            $entityManager->getConnection()->commit();

            return $envelope;
        } catch (\Throwable $exception) {
            $entityManager->getConnection()->rollBack();

            if ($exception instanceof HandlerFailedException) {
                // Remove all HandledStamp from the envelope so the retry will execute all handlers again.                 // When a handler fails, the queries of allegedly successful previous handlers just got rolled back.                 throw new HandlerFailedException($exception->getEnvelope()->withoutAll(HandledStamp::class)$exception->getNestedExceptions());
            }

            throw $exception;
        }
    }
}
$stamp = ErrorDetailsStamp::create($exception);

        $this->assertSame(\Exception::class$stamp->getExceptionClass());
        $this->assertSame('exception message', $stamp->getExceptionMessage());
        $this->assertEquals($flattenException$stamp->getFlattenException());
    }

    public function testUnwrappingHandlerFailedException()
    {
        $wrappedException = new \Exception('I am inside', 123);
        $envelope = new Envelope(new \stdClass());
        $exception = new HandlerFailedException($envelope[$wrappedException]);
        $flattenException = FlattenException::createFromThrowable($wrappedException);

        $stamp = ErrorDetailsStamp::create($exception);

        $this->assertSame(\Exception::class$stamp->getExceptionClass());
        $this->assertSame('I am inside', $stamp->getExceptionMessage());
        $this->assertSame(123, $stamp->getExceptionCode());
        $this->assertEquals($flattenException$stamp->getFlattenException());
    }

    public function testDeserialization()
    {
continue;
            }

            try {
                $handler = $handlerDescriptor->getHandler();
                $batchHandler = $handlerDescriptor->getBatchHandler();

                /** @var AckStamp $ackStamp */
                if ($batchHandler && $ackStamp = $envelope->last(AckStamp::class)) {
                    $ack = new Acknowledger(get_debug_type($batchHandler)static function D\Throwable $e = null, $result = null) use ($envelope$ackStamp$handlerDescriptor) {
                        if (null !== $e) {
                            $e = new HandlerFailedException($envelope[$e]);
                        } else {
                            $envelope = $envelope->with(HandledStamp::fromDescriptor($handlerDescriptor$result));
                        }

                        $ackStamp->ack($envelope$e);
                    });

                    $result = $this->callHandler($handler$message$ack$envelope->last(HandlerArgumentsStamp::class));

                    if (!\is_int($result) || 0 > $result) {
                        throw new LogicException(sprintf('A handler implementing BatchHandlerInterface must return the size of the current batch as a positive integer, "%s" returned from "%s".', \is_int($result) ? $result : get_debug_type($result)get_debug_type($batchHandler)));
                    }
Home | Imprint | This part of the site doesn't use cookies.