From 8700198bd911456e96a20c377714622b5bb0a49e Mon Sep 17 00:00:00 2001 From: Michel Date: Wed, 11 Feb 2026 10:40:15 +0100 Subject: [PATCH] fix: log constraint violation errors correctly (#550) --- CHANGELOG.md | 3 +++ CHANGELOG_de-DE.md | 3 +++ src/Util/IntrospectionProcessor.php | 19 +++++++++++++------ tests/Util/IntrospectionProcessorTest.php | 21 +++++++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65aa62220..d2999db59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# REPLACE_GLOBALLY_WITH_NEXT_VERSION +- Fixes an issue, where causes for validation errors were not logged correctly + # 8.9.1 - Fixes an issue, where required cookies were not displayed in the banner even though PayPal scripts had been loaded (shopware/SwagPayPal#506) - Fixes an issue, where languages not supported by PayPal did not fall back to a supported language (shopware/shopware#13950) diff --git a/CHANGELOG_de-DE.md b/CHANGELOG_de-DE.md index 920e35dc8..fa0f16227 100644 --- a/CHANGELOG_de-DE.md +++ b/CHANGELOG_de-DE.md @@ -1,3 +1,6 @@ +# REPLACE_GLOBALLY_WITH_NEXT_VERSION +- Behebt ein Problem, bei dem Ursachen für Validierungsfehler nicht korrekt protokolliert wurden + # 8.9.1 - Behebt ein Problem, bei dem erforderliche Cookies nicht im Banner angezeigt wurden, obwohl PayPal-Skripte geladen wurden (shopware/SwagPayPal#506) - Behebt ein Problem, bei dem nicht von PayPal unterstützte Sprachen nicht auf eine unterstützte Sprache korrigiert wurden (shopware/shopware#13950) diff --git a/src/Util/IntrospectionProcessor.php b/src/Util/IntrospectionProcessor.php index 54522bbe9..857a009c0 100644 --- a/src/Util/IntrospectionProcessor.php +++ b/src/Util/IntrospectionProcessor.php @@ -15,6 +15,7 @@ use Shopware\Core\Framework\HttpException; use Shopware\Core\Framework\Log\Package; use Shopware\Core\Framework\ShopwareHttpException; +use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException; use Swag\PayPal\Pos\Api\Exception\PosException; use Swag\PayPal\Pos\Client\AbstractClient as PosAbstractClient; use Swag\PayPal\RestApi\Client\AbstractClient; @@ -140,21 +141,27 @@ protected function getBacktrace(): array */ private function exceptionToContext(\Throwable $exception): array { - $context = [ - 'message' => $exception->getMessage(), - 'class' => $this->traceToClassString($exception->getTrace()[0]), - 'file' => $exception->getFile(), - 'line' => $exception->getLine(), - ]; + $context = ['message' => $exception->getMessage()]; if ($exception instanceof ShopwareHttpException) { $context['parameters'] = $exception->getParameters(); } + if ($exception instanceof ConstraintViolationException) { + foreach ($exception->getViolations() as $violation) { + $context['parameters']['violations'][] = (string) $violation; + } + } + if ($exception instanceof HttpException || $exception instanceof PosException) { $context['errorCode'] = $exception->getErrorCode(); } + // Order class, file and line at the end to make the exception & the most important information more readable in logs + $context['class'] = $this->traceToClassString($exception->getTrace()[0]); + $context['file'] = $exception->getFile(); + $context['line'] = $exception->getLine(); + if ($exception->getPrevious()) { $context['previous'] = $this->exceptionToContext($exception->getPrevious()); } diff --git a/tests/Util/IntrospectionProcessorTest.php b/tests/Util/IntrospectionProcessorTest.php index 681de3154..6c249d3f9 100644 --- a/tests/Util/IntrospectionProcessorTest.php +++ b/tests/Util/IntrospectionProcessorTest.php @@ -12,6 +12,7 @@ use Monolog\LogRecord; use PHPUnit\Framework\TestCase; use Shopware\Core\Framework\Log\Package; +use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException; use Shopware\Core\Kernel; use Swag\PayPal\Checkout\Payment\Handler\PayPalHandler; use Swag\PayPal\Pos\Api\Exception\PosException; @@ -21,6 +22,8 @@ use Swag\PayPal\RestApi\V2\Resource\OrderResource; use Swag\PayPal\Storefront\Controller\PayPalController; use Swag\PayPal\Util\IntrospectionProcessor; +use Symfony\Component\Validator\ConstraintViolation; +use Symfony\Component\Validator\ConstraintViolationList; /** * @internal @@ -270,5 +273,23 @@ public static function invokeWithExceptionDataProvider(): \Generator 'errorCode' => 'SWAG_PAYPAL__POS_EXCEPTION', ]], ]; + + yield 'ConstraintViolationException' => [ + ['exception' => new ConstraintViolationException(new ConstraintViolationList([new ConstraintViolation( + 'test message', + 'test message template with {{ type }}', + ['{{ type }}' => 'testParameter'], + '/root', + 'testProperty', + 'VIOLATION_TESTPROPERTY_INVALID' + )]), [])], + ['exception' => [ + 'message' => 'Caught 1 violation errors.', + 'parameters' => [ + 'count' => 1, + 'violations' => ["/root.testProperty:\n test message"], + ], + ]], + ]; } }