Skip to content

Commit a1ee726

Browse files
committed
ACP2E-3399: GraphQL Response for Order placement does not include the exception message
1 parent 853f85a commit a1ee726

File tree

12 files changed

+102
-51
lines changed

12 files changed

+102
-51
lines changed

app/code/Magento/GraphQl/Helper/Error/AggregateExceptionMessageFormatter.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2021 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -14,7 +14,6 @@
1414
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
1515
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1616
use Magento\Framework\Phrase;
17-
use Magento\QuoteGraphQl\Model\ErrorMapper;
1817

1918
/**
2019
* Class for formatting internally-thrown errors if they match allowed exception types or using a default message if not
@@ -24,7 +23,7 @@ class AggregateExceptionMessageFormatter
2423
/**
2524
* @var ExceptionMessageFormatterInterface[]
2625
*/
27-
private $messageFormatters;
26+
private array $messageFormatters;
2827

2928
/**
3029
* @param ExceptionMessageFormatterInterface[] $messageFormatters
@@ -55,19 +54,11 @@ public function getFormatted(
5554
ResolveInfo $info
5655
): ClientAware {
5756
foreach ($this->messageFormatters as $formatter) {
58-
$formatted = $formatter->getFormatted($e, $messagePrefix, $field, $context, $info);
59-
if ($formatted && !$e->getCode() && ($errorId = ErrorMapper::getErrorMessageId($e->getMessage()))) {
60-
$exceptionType = get_class($e);
61-
$formatted = new $exceptionType(
62-
__($e->getMessage()),
63-
$e,
64-
$errorId
65-
);
66-
}
67-
if ($formatted) {
57+
if ($formatted = $formatter->getFormatted($e, $messagePrefix, $field, $context, $info)) {
6858
return $formatted;
6959
}
7060
}
61+
7162
$message = $e->getCode() ? __($e->getMessage()) : $defaultMessage;
7263
return new GraphQlInputException($message, $e, $e->getCode());
7364
}

app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForCheckout.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1414
use Magento\Quote\Api\CartManagementInterface;
1515
use Magento\Quote\Model\Quote;
16+
use Magento\QuoteGraphQl\Model\ErrorMapper;
1617

1718
/**
1819
* Get cart
@@ -29,16 +30,24 @@ class GetCartForCheckout
2930
*/
3031
private GetCartForUser $getCartForUser;
3132

33+
/**
34+
* @var ErrorMapper
35+
*/
36+
private ErrorMapper $errorMapper;
37+
3238
/**
3339
* @param CheckCartCheckoutAllowance $checkoutAllowance
3440
* @param GetCartForUser $getCartForUser
41+
* @param ErrorMapper $errorMapper
3542
*/
3643
public function __construct(
3744
CheckCartCheckoutAllowance $checkoutAllowance,
38-
GetCartForUser $getCartForUser
45+
GetCartForUser $getCartForUser,
46+
ErrorMapper $errorMapper
3947
) {
4048
$this->checkoutAllowance = $checkoutAllowance;
4149
$this->getCartForUser = $getCartForUser;
50+
$this->errorMapper = $errorMapper;
4251
}
4352

4453
/**
@@ -66,7 +75,7 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
6675
throw new GraphQlInputException(
6776
__("Guest email for cart is missing."),
6877
null,
69-
ErrorMapper::getErrorMessageId('Guest email for cart is missing')
78+
$this->errorMapper->getErrorMessageId('Guest email for cart is missing')
7079
);
7180
}
7281
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);

app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,47 @@ class GetCartForUser
2424
/**
2525
* @var MaskedQuoteIdToQuoteIdInterface
2626
*/
27-
private $maskedQuoteIdToQuoteId;
27+
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId;
2828

2929
/**
3030
* @var CartRepositoryInterface
3131
*/
32-
private $cartRepository;
32+
private CartRepositoryInterface $cartRepository;
3333

3434
/**
3535
* @var IsActive
3636
*/
37-
private $isActive;
37+
private IsActive $isActive;
3838

3939
/**
4040
* @var UpdateCartCurrency
4141
*/
42-
private $updateCartCurrency;
42+
private UpdateCartCurrency $updateCartCurrency;
43+
44+
/**
45+
* @var ErrorMapper
46+
*/
47+
private ErrorMapper $errorMapper;
4348

4449
/**
4550
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
4651
* @param CartRepositoryInterface $cartRepository
4752
* @param IsActive $isActive
4853
* @param UpdateCartCurrency $updateCartCurrency
54+
* @param ErrorMapper $errorMapper
4955
*/
5056
public function __construct(
5157
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
5258
CartRepositoryInterface $cartRepository,
5359
IsActive $isActive,
54-
UpdateCartCurrency $updateCartCurrency
60+
UpdateCartCurrency $updateCartCurrency,
61+
ErrorMapper $errorMapper
5562
) {
5663
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
5764
$this->cartRepository = $cartRepository;
5865
$this->isActive = $isActive;
5966
$this->updateCartCurrency = $updateCartCurrency;
67+
$this->errorMapper = $errorMapper;
6068
}
6169

6270
/**
@@ -81,15 +89,15 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
8189
throw new GraphQlNoSuchEntityException(
8290
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash]),
8391
$exception,
84-
ErrorMapper::getErrorMessageId('Could not find a cart with ID')
92+
$this->errorMapper->getErrorMessageId('Could not find a cart with ID')
8593
);
8694
}
8795

8896
if (false === (bool)$this->isActive->execute($cart)) {
8997
throw new GraphQlNoSuchEntityException(
9098
__('The cart isn\'t active.'),
9199
null,
92-
ErrorMapper::getErrorMessageId('The cart isn\'t active')
100+
$this->errorMapper->getErrorMessageId('The cart isn\'t active')
93101
);
94102
}
95103

app/code/Magento/QuoteGraphQl/Model/ErrorMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ErrorMapper
7777
* @param string $message
7878
* @return int
7979
*/
80-
public static function getErrorMessageId(string $message): int
80+
public function getErrorMessageId(string $message): int
8181
{
8282
$code = self::ERROR_UNDEFINED_ID;
8383

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class EstimateShippingMethods implements ResolverInterface
3838
* @param ExtensibleDataObjectConverter $dataObjectConverter
3939
* @param ShippingMethodConverter $shippingMethodConverter
4040
* @param FormatMoneyTypeData $formatMoneyTypeData
41+
* @param ErrorMapper $errorMapper
4142
*/
4243
public function __construct(
4344
private MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
@@ -47,6 +48,7 @@ public function __construct(
4748
private ExtensibleDataObjectConverter $dataObjectConverter,
4849
private ShippingMethodConverter $shippingMethodConverter,
4950
private FormatMoneyTypeData $formatMoneyTypeData,
51+
private ErrorMapper $errorMapper
5052
) {
5153
}
5254

@@ -67,7 +69,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6769
]
6870
),
6971
$ex,
70-
ErrorMapper::getErrorMessageId('Could not find a cart with ID')
72+
$this->errorMapper->getErrorMessageId('Could not find a cart with ID')
7173
);
7274
}
7375
return $this->getAvailableShippingMethodsForAddress($args['input']['address'], $cart);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2023 Adobe
3+
* Copyright 2024 Adobe
44
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
@@ -33,13 +33,15 @@ class EstimateTotals implements ResolverInterface
3333
* @param AddressFactory $addressFactory
3434
* @param TotalsInformationManagementInterface $totalsInformationManagement
3535
* @param TotalsInformationInterfaceFactory $totalsInformationFactory
36+
* @param ErrorMapper $errorMapper
3637
*/
3738
public function __construct(
3839
private readonly MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
3940
private readonly CartRepositoryInterface $cartRepository,
4041
private readonly AddressFactory $addressFactory,
4142
private readonly TotalsInformationManagementInterface $totalsInformationManagement,
42-
private readonly TotalsInformationInterfaceFactory $totalsInformationFactory
43+
private readonly TotalsInformationInterfaceFactory $totalsInformationFactory,
44+
private readonly ErrorMapper $errorMapper
4345
) {
4446
}
4547

@@ -65,7 +67,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6567
]
6668
),
6769
$exception,
68-
ErrorMapper::getErrorMessageId('Could not find a cart with ID')
70+
$this->errorMapper->getErrorMessageId('Could not find a cart with ID')
6971
);
7072
}
7173

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,47 @@ class RemoveItemFromCart implements ResolverInterface
2828
/**
2929
* @var GetCartForUser
3030
*/
31-
private $getCartForUser;
31+
private GetCartForUser $getCartForUser;
3232

3333
/**
3434
* @var CartItemRepositoryInterface
3535
*/
36-
private $cartItemRepository;
36+
private CartItemRepositoryInterface $cartItemRepository;
3737

3838
/**
3939
* @var MaskedQuoteIdToQuoteId
4040
*/
41-
private $maskedQuoteIdToQuoteId;
41+
private MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId;
4242

4343
/**
4444
* @var ArgumentsProcessorInterface
4545
*/
46-
private $argsSelection;
46+
private ArgumentsProcessorInterface $argsSelection;
47+
48+
/**
49+
* @var ErrorMapper
50+
*/
51+
private ErrorMapper $errorMapper;
4752

4853
/**
4954
* @param GetCartForUser $getCartForUser
5055
* @param CartItemRepositoryInterface $cartItemRepository
5156
* @param MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId
5257
* @param ArgumentsProcessorInterface $argsSelection
58+
* @param ErrorMapper $errorMapper
5359
*/
5460
public function __construct(
5561
GetCartForUser $getCartForUser,
5662
CartItemRepositoryInterface $cartItemRepository,
5763
MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId,
58-
ArgumentsProcessorInterface $argsSelection
64+
ArgumentsProcessorInterface $argsSelection,
65+
ErrorMapper $errorMapper
5966
) {
6067
$this->getCartForUser = $getCartForUser;
6168
$this->cartItemRepository = $cartItemRepository;
6269
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
6370
$this->argsSelection = $argsSelection;
71+
$this->errorMapper = $errorMapper;
6472
}
6573

6674
/**
@@ -79,7 +87,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7987
throw new GraphQlNoSuchEntityException(
8088
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $maskedCartId]),
8189
$exception,
82-
ErrorMapper::getErrorMessageId('Could not find a cart with ID')
90+
$this->errorMapper->getErrorMessageId('Could not find a cart with ID')
8391
);
8492
}
8593

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Plugin;
9+
10+
use Magento\GraphQl\Helper\Error\AggregateExceptionMessageFormatter;
11+
use Magento\QuoteGraphQl\Model\ErrorMapper;
12+
13+
class ExceptionMessageDecorator
14+
{
15+
/**
16+
* @param ErrorMapper $errorDecorator
17+
*/
18+
public function __construct(readonly ErrorMapper $errorDecorator)
19+
{
20+
}
21+
22+
/**
23+
* @param AggregateExceptionMessageFormatter $subject
24+
* @param mixed $result
25+
* @return mixed
26+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
27+
*/
28+
public function afterGetFormatted(AggregateExceptionMessageFormatter $subject, mixed $result): mixed
29+
{
30+
if (!$result->getCode() && ($errorId = $this->errorDecorator->getErrorMessageId($result->getMessage()))) {
31+
$exceptionType = get_class($result);
32+
$result = new $exceptionType(
33+
__($result->getMessage()),
34+
$result,
35+
$errorId
36+
);
37+
}
38+
39+
return $result;
40+
}
41+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2018 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
@@ -91,4 +91,7 @@
9191
</argument>
9292
</arguments>
9393
</type>
94+
<type name="Magento\GraphQl\Helper\Error\AggregateExceptionMessageFormatter">
95+
<plugin name="exception_message_decorator" type="Magento\QuoteGraphQl\Plugin\ExceptionMessageDecorator" sortOrder="1"/>
96+
</type>
9497
</config>

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/PlaceOrderTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,6 @@ private function getQuery(string $maskedQuoteId): string
448448
order {
449449
order_number
450450
}
451-
errors {
452-
message
453-
code
454-
}
455451
}
456452
}
457453
QUERY;

0 commit comments

Comments
 (0)