Skip to content

[Messenger][FrameworkBundle] Add option to exclude stack trace from ErrorDetailsStamp #53454

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHANGELOG
* Add JsonEncoder services and configuration
* Add new `framework.property_info.with_constructor_extractor` option to allow enabling or disabling the constructor extractor integration
* Deprecate the `--show-arguments` option of the `container:debug` command, as arguments are now always shown
* Add `include_stack_trace_in_error` option that's used in `AddErrorDetailsStampListener`

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,9 @@ function ($a) {
->defaultNull()
->info('Transport name to send failed messages to (after all retries have failed).')
->end()
->booleanNode('include_stack_trace_in_error')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too crazy about the name of the option, maybe someone has a better suggestion.

->info('Whether to include a stack trace in the error details stamp.')
->end()
->arrayNode('retry_strategy')
->addDefaultsIfNotSet()
->beforeNormalization()
Expand Down Expand Up @@ -1702,6 +1705,10 @@ function ($a) {
->defaultNull()
->info('Transport name to send failed messages to (after all retries have failed).')
->end()
->booleanNode('include_stack_trace_in_error')
->defaultTrue()
->info('Whether to include a stack trace in the error details stamp.')
->end()
->arrayNode('stop_worker_on_signals')
->defaultValue([])
->info('A list of signals that should stop the worker; defaults to SIGTERM and SIGINT.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2332,6 +2332,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
}

$senderAliases = [];
$includeStackTraceInError = [];
$transportRetryReferences = [];
$transportRateLimiterReferences = [];
foreach ($config['transports'] as $name => $transport) {
Expand All @@ -2347,6 +2348,8 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
$container->setDefinition($transportId = 'messenger.transport.'.$name, $transportDefinition);
$senderAliases[$name] = $transportId;

$includeStackTraceInError[$name] = $transport['include_stack_trace_in_error'] ?? $config['include_stack_trace_in_error'];

if (null !== $transport['retry_strategy']['service']) {
$transportRetryReferences[$name] = new Reference($transport['retry_strategy']['service']);
} else {
Expand Down Expand Up @@ -2419,6 +2422,10 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
->replaceArgument(1, $sendersServiceLocator)
;

$container->getDefinition('messenger.failure.add_error_details_stamp_listener')
->replaceArgument(0, $includeStackTraceInError)
;

$container->getDefinition('messenger.retry.send_failed_message_for_retry_listener')
->replaceArgument(0, $sendersServiceLocator)
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@
->tag('monolog.logger', ['channel' => 'messenger'])

->set('messenger.failure.add_error_details_stamp_listener', AddErrorDetailsStampListener::class)
->args([
abstract_arg('include_stack_trace_in_error'),
])
->tag('kernel.event_subscriber')

->set('messenger.failure.send_failed_message_to_failure_transport_listener', SendFailedMessageToFailureTransportListener::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@
<xsd:attribute name="default-bus" type="xsd:string" />
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="failure-transport" type="xsd:string" />
<xsd:attribute name="include-stack-trace-in-error" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="messenger_serializer">
Expand Down Expand Up @@ -642,6 +643,7 @@
<xsd:attribute name="serializer" type="xsd:string" />
<xsd:attribute name="dsn" type="xsd:string" />
<xsd:attribute name="failure-transport" type="xsd:string" />
<xsd:attribute name="include-stack-trace-in-error" type="xsd:boolean" />
<xsd:attribute name="rate-limiter" type="xsd:string" />
</xsd:complexType>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
'default_bus' => null,
'buses' => ['messenger.bus.default' => ['default_middleware' => ['enabled' => true, 'allow_no_handlers' => false, 'allow_no_senders' => true], 'middleware' => []]],
'stop_worker_on_signals' => [],
'include_stack_trace_in_error' => true,
],
'disallow_search_engine_index' => true,
'http_client' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

$container->loadFromExtension('framework', [
'annotations' => false,
'http_method_override' => false,
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'messenger' => [
'include_stack_trace_in_error' => false,
'transports' => [
'sender.biz' => 'null://',
'sender.bar' => [
'dsn' => 'null://',
'include_stack_trace_in_error' => true,
],
'sender.foo' => 'null://',
],
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

$container->loadFromExtension('framework', [
'annotations' => false,
'http_method_override' => false,
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'messenger' => [
'transports' => [
'sender.biz' => 'null://',
'sender.bar' => [
'dsn' => 'null://',
'include_stack_trace_in_error' => false,
],
'sender.foo' => 'null://',
],
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:messenger include-stack-trace-in-error="false">
<framework:transport name="sender.biz" dsn="null://" />
<framework:transport name="sender.bar" dsn="null://" include-stack-trace-in-error="true" />
<framework:transport name="sender.foo" dsn="null://" />
</framework:messenger>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config http-method-override="false" handle-all-throwables="true">
<framework:annotations enabled="false" />
<framework:php-errors log="true" />
<framework:messenger>
<framework:transport name="sender.biz" dsn="null://" />
<framework:transport name="sender.bar" dsn="null://" include-stack-trace-in-error="false" />
<framework:transport name="sender.foo" dsn="null://" />
</framework:messenger>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
framework:
annotations: false
http_method_override: false
handle_all_throwables: true
php_errors:
log: true
messenger:
include_stack_trace_in_error: false
transports:
sender.biz: 'null://'
sender.bar:
dsn: 'null://'
include_stack_trace_in_error: true
sender.foo: 'null://'
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
framework:
annotations: false
http_method_override: false
handle_all_throwables: true
php_errors:
log: true
messenger:
transports:
sender.biz: 'null://'
sender.bar:
dsn: 'null://'
include_stack_trace_in_error: false
sender.foo: 'null://'
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,17 @@ public function testMessengerInvalidWildcardRouting()
$this->createContainerFromFile('messenger_routing_invalid_transport');
}

/**
* @testWith ["messenger_with_include_stack_trace_true", {"sender.biz": true, "sender.bar": false, "sender.foo": true}]
* ["messenger_with_include_stack_trace_false", {"sender.biz": false, "sender.bar": true, "sender.foo": false}]
*/
public function testMessengerWithIncludeStackTrace(string $file, array $expected)
{
$container = $this->createContainerFromFile($file);

$this->assertSame($expected, $container->getDefinition('messenger.failure.add_error_details_stamp_listener')->getArgument(0));
}

public function testTranslator()
{
$container = $this->createContainerFromFile('full');
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `$includeStackTrace` parameters to `AddErrorDetailsStampListener` and `ErrorDetailsStamp` to allow excluding the stack trace from the `ErrorDetailsStamp`

7.2
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@

final class AddErrorDetailsStampListener implements EventSubscriberInterface
{
/**
* @param array<string, bool> $includeStackTrace
*/
public function __construct(
private readonly array $includeStackTrace = [],
) {
}

public function onMessageFailed(WorkerMessageFailedEvent $event): void
{
$stamp = ErrorDetailsStamp::create($event->getThrowable());
$stamp = ErrorDetailsStamp::create($event->getThrowable(), $this->includeStackTrace[$event->getReceiverName()] ?? true);
$previousStamp = $event->getEnvelope()->last(ErrorDetailsStamp::class);

// Do not append duplicate information
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Messenger/Stamp/ErrorDetailsStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public function __construct(
) {
}

public static function create(\Throwable $throwable): self
public static function create(\Throwable $throwable, bool $includeStackTrace = true): self
{
if ($throwable instanceof HandlerFailedException) {
$throwable = $throwable->getPrevious();
}

$flattenException = null;
if (class_exists(FlattenException::class)) {
if ($includeStackTrace && class_exists(FlattenException::class)) {
$flattenException = FlattenException::createFromThrowable($throwable);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ public function testExceptionDetailsAreAdded()
$this->assertEquals($expectedStamp, $event->getEnvelope()->last(ErrorDetailsStamp::class));
}

public function testExceptionDetailsWithoutStackTraceAreAdded()
{
$listener = new AddErrorDetailsStampListener(['my_receiver' => false]);

$envelope = new Envelope(new \stdClass());
$exception = new \Exception('It failed!');
$event = new WorkerMessageFailedEvent($envelope, 'my_receiver', $exception);
$expectedStamp = ErrorDetailsStamp::create($exception, false);

$listener->onMessageFailed($event);

$this->assertEquals($expectedStamp, $event->getEnvelope()->last(ErrorDetailsStamp::class));
}

public function testWorkerAddsNewErrorDetailsStampOnFailure()
{
$listener = new AddErrorDetailsStampListener();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ public function testUnwrappingHandlerFailedException()
$this->assertEquals($flattenException, $stamp->getFlattenException());
}

public function testFlattenExceptionIsNotIncluded()
{
$exception = new \Exception('exception message');

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

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

public function testDeserialization()
{
$exception = new \Exception('exception message');
Expand Down