Skip to content

[Messenger] Add $previous to failed message details #52921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ToshY opened this issue Dec 6, 2023 · 4 comments · Fixed by #52951
Closed

[Messenger] Add $previous to failed message details #52921

ToshY opened this issue Dec 6, 2023 · 4 comments · Fixed by #52951

Comments

@ToshY
Copy link
Contributor

ToshY commented Dec 6, 2023

Description

Problem

Currently if an exception is thrown inside the handler without defining either message or code arguments, and explicetely only using previous, the messenger:failed:show command shows an output containing an exception like so:

messenger:failed:show 118 -vvv

Failed Message Details
======================

 ------------- -----------------------------------------------------------------------------
  Class         App\Message\TestLoggerMessage
  Message Id    118
  Failed at     2023-12-06 22:37:42
  Error
  Error Code    0
  Error Class   Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException
  Transport     async
 ------------- -----------------------------------------------------------------------------

 Message history:
  * Message failed at 2023-12-06 22:37:42 and was redelivered

Message:
========

App\Message\TestLoggerMessage^ {}

Exception:
==========

Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException^ {
  message: ""
  code: 0
  file: "/app/src/MessageHandler/TestLoggerMessageHandler.php"
  line: 20
  trace: {
    /app/src/MessageHandler/TestLoggerMessageHandler.php:20 {
      App\MessageHandler\TestLoggerMessageHandler->__invoke(TestLoggerMessage $message): void^
      ›
      › throw new UnrecoverableMessageHandlingException(
      ›     previous: $exception
    }
    /app/vendor/symfony/messenger/Middleware/HandleMessageMiddleware.php:157 { …}
    /app/vendor/symfony/messenger/Middleware/HandleMessageMiddleware.php:96 { …}
    /app/vendor/symfony/messenger/Middleware/SendMessageMiddleware.php:77 { …}
    /app/vendor/symfony/doctrine-bridge/Messenger/DoctrineOpenTransactionLoggerMiddleware.php:48 { …}
    /app/vendor/symfony/doctrine-bridge/Messenger/AbstractDoctrineMiddleware.php:45 { …}
    /app/vendor/symfony/doctrine-bridge/Messenger/DoctrineCloseConnectionMiddleware.php:31 { …}
    /app/vendor/symfony/doctrine-bridge/Messenger/AbstractDoctrineMiddleware.php:45 { …}
    /app/vendor/symfony/doctrine-bridge/Messenger/DoctrinePingConnectionMiddleware.php:34 { …}
    /app/vendor/symfony/doctrine-bridge/Messenger/AbstractDoctrineMiddleware.php:45 { …}
    /app/src/Service/Application/Messenger/Middleware/LockMessageReleaseMiddleware.php:22 { …}
    /app/src/Service/Application/Messenger/Middleware/LockMessageMiddleware.php:45 { …}
    /app/src/Service/Application/Messenger/Middleware/AuditMiddleware.php:36 { …}
    /app/vendor/symfony/messenger/Middleware/FailedMessageProcessingMiddleware.php:34 { …}
    /app/vendor/symfony/messenger/Middleware/DispatchAfterCurrentBusMiddleware.php:68 { …}
    /app/vendor/symfony/messenger/Middleware/RejectRedeliveredMessageMiddleware.php:41 { …}
    /app/vendor/symfony/messenger/Middleware/AddBusNameStampMiddleware.php:37 { …}
    /app/vendor/symfony/messenger/Middleware/TraceableMiddleware.php:40 { …}
    /app/vendor/symfony/messenger/MessageBus.php:70 { …}
    /app/vendor/symfony/messenger/TraceableMessageBus.php:38 { …}
    /app/vendor/symfony/messenger/RoutableMessageBus.php:54 { …}
    /app/vendor/symfony/messenger/Worker.php:160 { …}
    /app/vendor/symfony/messenger/Worker.php:109 { …}
    /app/vendor/symfony/messenger/Command/ConsumeMessagesCommand.php:238 { …}
    /app/vendor/symfony/console/Command/Command.php:326 { …}
    /app/vendor/symfony/console/Application.php:1081 { …}
    /app/vendor/symfony/framework-bundle/Console/Application.php:91 { …}
    /app/vendor/symfony/console/Application.php:320 { …}
    /app/vendor/symfony/framework-bundle/Console/Application.php:80 { …}
    /app/vendor/symfony/console/Application.php:174 { …}
    /app/bin/console:43 { …}
  }
}

Now debugging this message has become quite a bit harder, as nothing is shown regarding the previous exception (not even the previous message or code).

Possible solution

Add Caster::PREFIX_VIRTUAL.'previous' => $flattenException->getPrevious(), at AbstractFailedMessagesCommand#L190.

Example

App\Message\TestLoggerMessage

<?php

namespace App\Message;

final class TestLoggerMessage
{
    public function __construct()
    {
    }
}

App\MessageHandler\TestLoggerMessageHandler

<?php

declare(strict_types=1);

namespace App\MessageHandler;

use App\Message\TestLoggerMessage;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;

#[AsMessageHandler]
final class TestLoggerMessageHandler
{
    public function __invoke(TestLoggerMessage $message): void
    {
        $exception = new \Exception('This is not good!', 1);

        // ...

        throw new UnrecoverableMessageHandlingException(
            previous: $exception
        );
    }
}
@OskarStark
Copy link
Contributor

Can you submit a PR?

@OskarStark OskarStark changed the title [Messenger] Add "previous" to failed message details [Messenger] Add $previous to failed message details Dec 8, 2023
@ToshY
Copy link
Contributor Author

ToshY commented Dec 8, 2023

Hey @OskarStark 👋

Ofcourse! But could you tell me which branch I need to target? I was under the assumption to target 6.4 because it is stated in the contribution docs to target 6.4 (which is the previous minor version), but the PR template tells me to target 7.1 for features. So that's somewhat confusing.

@xabbuh
Copy link
Member

xabbuh commented Dec 8, 2023

@ToshY 7.1 is indeed the correct base branch for new features. I have submitted symfony/symfony-docs#19238 to fix the docs.

@ToshY
Copy link
Contributor Author

ToshY commented Dec 8, 2023

@xabbuh Thanks for the clarification (and update of the docs) 🙇‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants