diff --git a/lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php b/lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php index 6effa1e972193..01a4bd27b4c4f 100644 --- a/lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php +++ b/lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php @@ -53,12 +53,17 @@ public function handle(array $errors, callable $formatter): array } foreach ($errors as $error) { - $this->log($error); + $isClientInputError = $this->isClientInputError($error); + if (!$isClientInputError) { + $this->logger->error($error); + } $previousError = $error->getPrevious(); if ($previousError instanceof AggregateExceptionInterface && !empty($previousError->getErrors())) { $aggregatedErrors = $previousError->getErrors(); foreach ($aggregatedErrors as $aggregatedError) { - $this->logger->error($aggregatedError); + if (!$isClientInputError) { + $this->logger->error($aggregatedError); + } $formattedErrors[] = $formatter($aggregatedError); } } else { @@ -69,19 +74,15 @@ public function handle(array $errors, callable $formatter): array } /** - * Log error. + * Check whether the error was caused by invalid client input and therefore must not be logged. * * @param Error $error - * @return void + * @return bool */ - private function log(Error $error): void + private function isClientInputError(Error $error): bool { $extensions = $error->getExtensions(); - $category = $extensions['category'] ?? null; - if (GraphQlInputException::EXCEPTION_CATEGORY === $category) { - return; - } - $this->logger->error($error); + return GraphQlInputException::EXCEPTION_CATEGORY === ($extensions['category'] ?? null); } } diff --git a/lib/internal/Magento/Framework/GraphQl/Test/Unit/Query/ErrorHandlerTest.php b/lib/internal/Magento/Framework/GraphQl/Test/Unit/Query/ErrorHandlerTest.php index fe31753381e2f..f92c80a19ecc5 100644 --- a/lib/internal/Magento/Framework/GraphQl/Test/Unit/Query/ErrorHandlerTest.php +++ b/lib/internal/Magento/Framework/GraphQl/Test/Unit/Query/ErrorHandlerTest.php @@ -9,6 +9,8 @@ use GraphQL\Error\Error; use Magento\Framework\App\State as AppState; +use Magento\Framework\Exception\InputException; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlServerException; use Magento\Framework\GraphQl\Query\ErrorHandler; @@ -54,6 +56,12 @@ public static function errorsDataProvider(): array { $inputException = new GraphQlInputException(__('Input error')); $serverException = new GraphQlServerException(__('Server error')); + $aggregatedInputException = (new GraphQlInputException(__('Input error'))) + ->addError(new LocalizedException(__('Child input error 1'))) + ->addError(new LocalizedException(__('Child input error 2'))); + $aggregatedServerException = (new InputException(__('Aggregate error'))) + ->addError(__('Child error 1')) + ->addError(__('Child error 2')); return [ [ [new Error('Error 1'), new Error('Error 2')], AppState::MODE_DEVELOPER, 2 @@ -73,6 +81,35 @@ public static function errorsDataProvider(): array [ [new Error('Error 1', previous: $serverException)], AppState::MODE_DEVELOPER, 1 ], + [ + [new Error('Error 1', previous: $aggregatedInputException)], AppState::MODE_DEVELOPER, 0 + ], + [ + [new Error('Error 1', previous: $aggregatedServerException)], AppState::MODE_DEVELOPER, 3 + ], + ]; + } + + public function testHandleReportsAggregatedClientInputErrorsWithoutLogging(): void + { + $childErrors = [ + new LocalizedException(__('Child input error 1')), + new LocalizedException(__('Child input error 2')), ]; + $exception = new GraphQlInputException(__('Input error')); + foreach ($childErrors as $childError) { + $exception->addError($childError); + } + $this->appStateMock->expects(self::atLeastOnce()) + ->method('getMode') + ->willReturn(AppState::MODE_DEVELOPER); + $this->loggerMock->expects(self::never())->method('error'); + + $formattedErrors = $this->errorHandler->handle( + [new Error('Error 1', previous: $exception)], + fn ($error) => $error + ); + + self::assertSame($childErrors, $formattedErrors); } }