Skip to content
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
21 changes: 11 additions & 10 deletions lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}