Skip to content

Commit 853f85a

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

File tree

11 files changed

+191
-137
lines changed

11 files changed

+191
-137
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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;
1718

1819
/**
1920
* Class for formatting internally-thrown errors if they match allowed exception types or using a default message if not
@@ -55,10 +56,19 @@ public function getFormatted(
5556
): ClientAware {
5657
foreach ($this->messageFormatters as $formatter) {
5758
$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+
}
5867
if ($formatted) {
5968
return $formatted;
6069
}
6170
}
62-
return new GraphQlInputException($defaultMessage, $e);
71+
$message = $e->getCode() ? __($e->getMessage()) : $defaultMessage;
72+
return new GraphQlInputException($message, $e, $e->getCode());
6373
}
6474
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
6363

6464
if (null === $customerId || 0 === $customerId) {
6565
if (!$cart->getCustomerEmail()) {
66-
throw new GraphQlInputException(__("Guest email for cart is missing."));
66+
throw new GraphQlInputException(
67+
__("Guest email for cart is missing."),
68+
null,
69+
ErrorMapper::getErrorMessageId('Guest email for cart is missing')
70+
);
6771
}
6872
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
6973
}

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

Lines changed: 11 additions & 4 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 2018 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -14,6 +14,7 @@
1414
use Magento\Quote\Api\CartRepositoryInterface;
1515
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1616
use Magento\Quote\Model\Quote;
17+
use Magento\QuoteGraphQl\Model\ErrorMapper;
1718

1819
/**
1920
* Get cart
@@ -78,12 +79,18 @@ public function execute(string $cartHash, ?int $customerId, int $storeId): Quote
7879
$cart = $this->cartRepository->get($cartId);
7980
} catch (NoSuchEntityException $exception) {
8081
throw new GraphQlNoSuchEntityException(
81-
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash])
82+
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash]),
83+
$exception,
84+
ErrorMapper::getErrorMessageId('Could not find a cart with ID')
8285
);
8386
}
8487

8588
if (false === (bool)$this->isActive->execute($cart)) {
86-
throw new GraphQlNoSuchEntityException(__('The cart isn\'t active.'));
89+
throw new GraphQlNoSuchEntityException(
90+
__('The cart isn\'t active.'),
91+
null,
92+
ErrorMapper::getErrorMessageId('The cart isn\'t active')
93+
);
8794
}
8895

8996
$cart = $this->updateCartCurrency->execute($cart, $storeId);
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
7+
namespace Magento\QuoteGraphQl\Model;
8+
9+
class ErrorMapper
10+
{
11+
/**
12+
* Error message codes
13+
*/
14+
private const ERROR_CART_NOT_FOUND = 'CART_NOT_FOUND';
15+
private const ERROR_CART_NOT_ACTIVE = 'CART_NOT_ACTIVE';
16+
private const ERROR_GUEST_EMAIL_MISSING = 'GUEST_EMAIL_MISSING';
17+
private const ERROR_UNABLE_TO_PLACE_ORDER = 'UNABLE_TO_PLACE_ORDER';
18+
private const ERROR_UNDEFINED = 'UNDEFINED';
19+
20+
/**
21+
* Error message codes ids
22+
*/
23+
private const ERROR_CART_NOT_FOUND_ID = 1001;
24+
private const ERROR_CART_NOT_ACTIVE_ID = 1002;
25+
private const ERROR_GUEST_EMAIL_MISSING_ID = 1003;
26+
private const ERROR_UNABLE_TO_PLACE_ORDER_ID = 1004;
27+
private const ERROR_UNDEFINED_ID = 1005;
28+
29+
/**
30+
* List of error messages and codes ids.
31+
*/
32+
private const MESSAGE_IDS = [
33+
'Could not find a cart with ID' => self::ERROR_CART_NOT_FOUND_ID,
34+
'The cart isn\'t active' => self::ERROR_CART_NOT_ACTIVE_ID,
35+
'Guest email for cart is missing' => self::ERROR_GUEST_EMAIL_MISSING_ID,
36+
'A server error stopped your order from being placed. Please try to place your order again' =>
37+
self::ERROR_UNABLE_TO_PLACE_ORDER_ID,
38+
'Some addresses can\'t be used due to the configurations for specific countries' =>
39+
self::ERROR_UNABLE_TO_PLACE_ORDER_ID,
40+
'The shipping method is missing. Select the shipping method and try again' =>
41+
self::ERROR_UNABLE_TO_PLACE_ORDER_ID,
42+
'Please check the billing address information' => self::ERROR_UNABLE_TO_PLACE_ORDER_ID,
43+
'Enter a valid payment method and try again' => self::ERROR_UNABLE_TO_PLACE_ORDER_ID,
44+
'Some of the products are out of stock' => self::ERROR_UNABLE_TO_PLACE_ORDER_ID,
45+
];
46+
47+
/**
48+
* List of error message ids and codes.
49+
*/
50+
private const MESSAGE_CODE_IDS = [
51+
self::ERROR_CART_NOT_FOUND_ID => self::ERROR_CART_NOT_FOUND,
52+
self::ERROR_CART_NOT_ACTIVE_ID => self::ERROR_CART_NOT_ACTIVE,
53+
self::ERROR_GUEST_EMAIL_MISSING_ID => self::ERROR_GUEST_EMAIL_MISSING,
54+
self::ERROR_UNABLE_TO_PLACE_ORDER_ID => self::ERROR_UNABLE_TO_PLACE_ORDER,
55+
self::ERROR_UNDEFINED_ID => self::ERROR_UNDEFINED
56+
];
57+
58+
/**
59+
* List of error messages and codes.
60+
*/
61+
private const MESSAGE_CODES = [
62+
'Could not find a cart with ID' => self::ERROR_CART_NOT_FOUND,
63+
'The cart isn\'t active' => self::ERROR_CART_NOT_ACTIVE,
64+
'Guest email for cart is missing' => self::ERROR_GUEST_EMAIL_MISSING,
65+
'A server error stopped your order from being placed. Please try to place your order again' =>
66+
self::ERROR_UNABLE_TO_PLACE_ORDER,
67+
'Some addresses can\'t be used due to the configurations for specific countries' =>
68+
self::ERROR_UNABLE_TO_PLACE_ORDER,
69+
'The shipping method is missing. Select the shipping method and try again' =>
70+
self::ERROR_UNABLE_TO_PLACE_ORDER,
71+
'Please check the billing address information' => self::ERROR_UNABLE_TO_PLACE_ORDER,
72+
'Enter a valid payment method and try again' => self::ERROR_UNABLE_TO_PLACE_ORDER,
73+
'Some of the products are out of stock' => self::ERROR_UNABLE_TO_PLACE_ORDER,
74+
];
75+
76+
/**
77+
* @param string $message
78+
* @return int
79+
*/
80+
public static function getErrorMessageId(string $message): int
81+
{
82+
$code = self::ERROR_UNDEFINED_ID;
83+
84+
$matchedCodes = array_filter(
85+
self::MESSAGE_IDS,
86+
function ($key) use ($message) {
87+
return str_contains($message, $key);
88+
},
89+
ARRAY_FILTER_USE_KEY
90+
);
91+
92+
if (!empty($matchedCodes)) {
93+
$code = current($matchedCodes);
94+
}
95+
96+
return $code;
97+
}
98+
99+
/**
100+
* @param int $errorId
101+
* @return string
102+
*/
103+
public static function getMessageCode(int $errorId): string
104+
{
105+
return self::MESSAGE_CODE_IDS[$errorId] ?? self::ERROR_UNDEFINED;
106+
}
107+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
2222
use Magento\Quote\Model\Quote\AddressFactory;
2323
use Magento\Quote\Model\Cart\ShippingMethodConverter;
24+
use Magento\QuoteGraphQl\Model\ErrorMapper;
2425
use Magento\QuoteGraphQl\Model\FormatMoneyTypeData;
2526

2627
/**
@@ -64,7 +65,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6465
[
6566
'masked_id' => $args['input']['cart_id']
6667
]
67-
)
68+
),
69+
$ex,
70+
ErrorMapper::getErrorMessageId('Could not find a cart with ID')
6871
);
6972
}
7073
return $this->getAvailableShippingMethodsForAddress($args['input']['address'], $cart);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
2121
use Magento\Quote\Model\Quote\Address;
2222
use Magento\Quote\Model\Quote\AddressFactory;
23+
use Magento\QuoteGraphQl\Model\ErrorMapper;
2324

2425
/**
2526
* Apply address and shipping method to totals estimate and return the quote
@@ -62,7 +63,9 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6263
[
6364
'masked_id' => $args['input']['cart_id']
6465
]
65-
)
66+
),
67+
$exception,
68+
ErrorMapper::getErrorMessageId('Could not find a cart with ID')
6669
);
6770
}
6871

0 commit comments

Comments
 (0)