Skip to content

Commit 4890bc2

Browse files
ACPT-1587: Send GraphQl errors to New Relic
Sending errors from Magento\Framework\GraphQl\Query\ErrorHandler now too
1 parent 24883b3 commit 4890bc2

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

app/code/Magento/GraphQlNewRelic/Plugin/ReportError.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
namespace Magento\GraphQlNewRelic\Plugin;
99

10-
use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
10+
use GraphQL\Error\Error;
11+
use Magento\Framework\GraphQl\Query\ErrorHandler;
1112
use Magento\NewRelicReporting\Model\NewRelicWrapper;
1213

1314
/**
@@ -24,18 +25,17 @@ public function __construct(private NewRelicWrapper $newRelicWrapper) {
2425
/**
2526
* Sends error from GraphQL to New Relic
2627
*
27-
* @param ExceptionFormatter $subject
28-
* @param \Throwable $exception
29-
* @param string|null $internalErrorMessage
28+
* @param ErrorHandler $subject
29+
* @param Error[] $errors
30+
* @param callable $formatter
3031
* @return null
3132
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3233
*/
33-
public function beforeCheck(
34-
ExceptionFormatter $subject,
35-
\Throwable $exception,
36-
string $internalErrorMessage= null
34+
public function beforeHandle(ErrorHandler $subject, array $errors, callable $formatter
3735
): null {
38-
$this->newRelicWrapper->reportError($exception);
36+
if (!empty($errors)) {
37+
$this->newRelicWrapper->reportError($errors[0]); // Note: We only log the first error because performance
38+
}
3939
return null;
4040
}
4141
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQlNewRelic\Plugin;
9+
10+
use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
11+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
12+
13+
/**
14+
* Plugin that sends GraphQL Errors to New Relic
15+
*/
16+
class ReportException
17+
{
18+
/**
19+
* @param NewRelicWrapper $newRelicWrapper
20+
*/
21+
public function __construct(private NewRelicWrapper $newRelicWrapper) {
22+
}
23+
24+
/**
25+
* Sends error from GraphQL to New Relic
26+
*
27+
* @param ExceptionFormatter $subject
28+
* @param \Throwable $exception
29+
* @param string|null $internalErrorMessage
30+
* @return null
31+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
32+
*/
33+
public function beforeCheck(
34+
ExceptionFormatter $subject,
35+
\Throwable $exception,
36+
string $internalErrorMessage= null
37+
): null {
38+
$this->newRelicWrapper->reportError($exception);
39+
return null;
40+
}
41+
}

app/code/Magento/GraphQlNewRelic/etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
</arguments>
1515
</type>
1616
<type name="Magento\Framework\GraphQl\Exception\ExceptionFormatter">
17+
<plugin name="report-exception-to-new-relic" type="Magento\GraphQlNewRelic\Plugin\ReportException" sortOrder="50"/>
18+
</type>
19+
<type name="Magento\Framework\GraphQl\Query\ErrorHandler">
1720
<plugin name="report-error-to-new-relic" type="Magento\GraphQlNewRelic\Plugin\ReportError" sortOrder="50"/>
1821
</type>
1922
</config>

0 commit comments

Comments
 (0)