Skip to content

Commit 3774f89

Browse files
committed
ACP2E-4031: [QUANS] Confirm GQL order placement exception handling behaviour reversion
1 parent 180ec2f commit 3774f89

File tree

4 files changed

+82
-33
lines changed

4 files changed

+82
-33
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model;
9+
10+
use Magento\Framework\Exception\AuthorizationException;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\GraphQl\Helper\Error\AggregateExceptionMessageFormatter;
16+
17+
class OrderErrorProcessor
18+
{
19+
/**
20+
* @param AggregateExceptionMessageFormatter $errorMessageFormatter
21+
* @param ErrorMapper $errorMapper
22+
*/
23+
public function __construct(
24+
private readonly AggregateExceptionMessageFormatter $errorMessageFormatter,
25+
private readonly ErrorMapper $errorMapper
26+
) {
27+
}
28+
29+
/**
30+
* @param LocalizedException $exception
31+
* @param Field $field
32+
* @param $context
33+
* @param ResolveInfo $info
34+
* @return array
35+
* @throws GraphQlAuthorizationException
36+
* @throws QuoteException
37+
*/
38+
public function execute(
39+
LocalizedException $exception,
40+
Field $field, $context,
41+
ResolveInfo $info
42+
): array {
43+
if ($exception instanceof AuthorizationException) {
44+
throw new GraphQlAuthorizationException(
45+
__($exception->getMessage())
46+
);
47+
}
48+
$exception = $this->errorMessageFormatter->getFormatted(
49+
$exception,
50+
__('Unable to place order: A server error stopped your order from being placed. ' .
51+
'Please try to place your order again'),
52+
'Unable to place order',
53+
$field,
54+
$context,
55+
$info
56+
);
57+
$exceptionCode = $exception->getCode();
58+
if (!$exceptionCode) {
59+
$exceptionCode = $this->errorMapper->getErrorMessageId($exception->getRawMessage());
60+
}
61+
throw new QuoteException(__($exception->getMessage()), $exception, $exceptionCode);
62+
}
63+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/PlaceOrder.php

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,17 @@
1010
use Magento\Framework\Exception\AuthorizationException;
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Magento\Framework\GraphQl\Config\Element\Field;
13-
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1413
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1514
use Magento\Framework\GraphQl\Query\ResolverInterface;
1615
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17-
use Magento\GraphQl\Helper\Error\AggregateExceptionMessageFormatter;
1816
use Magento\QuoteGraphQl\Model\Cart\GetCartForCheckout;
1917
use Magento\QuoteGraphQl\Model\Cart\PlaceOrder as PlaceOrderModel;
20-
use Magento\QuoteGraphQl\Model\ErrorMapper;
21-
use Magento\QuoteGraphQl\Model\QuoteException;
18+
use Magento\QuoteGraphQl\Model\OrderErrorProcessor;
2219
use Magento\Sales\Api\OrderRepositoryInterface;
2320
use Magento\SalesGraphQl\Model\Formatter\Order as OrderFormatter;
2421

2522
/**
2623
* Resolver for placing order after payment method has already been set
27-
*
28-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2924
*/
3025
class PlaceOrder implements ResolverInterface
3126
{
@@ -34,16 +29,14 @@ class PlaceOrder implements ResolverInterface
3429
* @param PlaceOrderModel $placeOrder
3530
* @param OrderRepositoryInterface $orderRepository
3631
* @param OrderFormatter $orderFormatter
37-
* @param AggregateExceptionMessageFormatter $errorMessageFormatter
38-
* @param ErrorMapper $errorMapper
32+
* @param OrderErrorProcessor $orderErrorProcessor
3933
*/
4034
public function __construct(
4135
private readonly GetCartForCheckout $getCartForCheckout,
4236
private readonly PlaceOrderModel $placeOrder,
4337
private readonly OrderRepositoryInterface $orderRepository,
4438
private readonly OrderFormatter $orderFormatter,
45-
private readonly AggregateExceptionMessageFormatter $errorMessageFormatter,
46-
private readonly ErrorMapper $errorMapper
39+
private readonly OrderErrorProcessor $orderErrorProcessor
4740
) {
4841
}
4942

@@ -63,26 +56,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, ?array $value
6356
$cart = $this->getCartForCheckout->execute($maskedCartId, $userId, $storeId);
6457
$orderId = $this->placeOrder->execute($cart, $maskedCartId, $userId);
6558
$order = $this->orderRepository->get($orderId);
66-
} catch (AuthorizationException $exception) {
67-
throw new GraphQlAuthorizationException(
68-
__($exception->getMessage())
69-
);
70-
} catch (LocalizedException $exception) {
71-
$exception = $this->errorMessageFormatter->getFormatted(
72-
$exception,
73-
__('Unable to place order: A server error stopped your order from being placed. ' .
74-
'Please try to place your order again'),
75-
'Unable to place order',
76-
$field,
77-
$context,
78-
$info
79-
);
80-
$exceptionCode = $exception->getCode();
81-
if (!$exceptionCode) {
82-
$exceptionCode = $this->errorMapper->getErrorMessageId($exception->getMessage());
83-
}
84-
85-
throw new QuoteException(__($exception->getMessage()), $exception, $exceptionCode);
59+
} catch (AuthorizationException|LocalizedException $exception) {
60+
return $this->orderErrorProcessor->execute($exception, $field, $context, $info);
8661
}
8762

8863
return [
@@ -91,7 +66,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, ?array $value
9166
// @deprecated The order_id field is deprecated, use order_number instead
9267
'order_id' => $order?->getIncrementId(),
9368
],
94-
'orderV2' => $order ? $this->orderFormatter->format($order) : null
69+
'orderV2' => $order ? $this->orderFormatter->format($order) : null,
9570
];
9671
}
9772
}

app/code/Magento/QuoteGraphQl/etc/graphql/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
</argument>
4545
</arguments>
4646
</virtualType>
47-
<type name="Magento\QuoteGraphQl\Model\Resolver\PlaceOrder">
47+
<type name="Magento\QuoteGraphQl\Model\OrderErrorProcessor">
4848
<arguments>
4949
<argument name="errorMessageFormatter" xsi:type="object">Magento\QuoteGraphQl\Helper\Error\PlaceOrderMessageFormatter</argument>
5050
</arguments>

lib/internal/Magento/Framework/GraphQl/Query/QueryProcessor.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function process(
8888
}
8989

9090
$rootValue = null;
91-
return GraphQL::executeQuery(
91+
$executionResult = GraphQL::executeQuery(
9292
$schema,
9393
$source,
9494
$rootValue,
@@ -100,5 +100,16 @@ public function process(
100100
)->toArray(
101101
(int) ($this->exceptionFormatter->shouldShowDetail() ? DebugFlag::INCLUDE_DEBUG_MESSAGE : false)
102102
);
103+
if (!empty($executionResult['errors'])) {
104+
foreach ($executionResult['errors'] as $error) {
105+
if (isset($error['extensions']['error_code'])) {
106+
$executionResult['data']['errors'][] = [
107+
'message' => $error['message'],
108+
'code' => $error['extensions']['error_code']
109+
];
110+
}
111+
}
112+
}
113+
return $executionResult;
103114
}
104115
}

0 commit comments

Comments
 (0)