Skip to content

Commit 27e4c96

Browse files
committed
ACP2E-3399: GraphQL Response for Order placement does not include the exception message
1 parent 3566dd9 commit 27e4c96

File tree

8 files changed

+136
-77
lines changed

8 files changed

+136
-77
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class ErrorMapper
7474
];
7575

7676
/**
77+
* Transforms a message into a corresponding id
78+
*
7779
* @param string $message
7880
* @return int
7981
*/
@@ -95,13 +97,4 @@ function ($key) use ($message) {
9597

9698
return $code;
9799
}
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-
}
107100
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function getExtensions(): array
2020
{
2121
$extensions['category'] = $this->getCategory();
2222
if ($this->code) {
23-
$extensions['error_code'] = ErrorMapper::getMessageCode($this->code);
23+
$extensions['error_code'] = ErrorMapper::MESSAGE_CODE_IDS[$this->code] ?? ErrorMapper::ERROR_UNDEFINED;
2424
}
2525

2626
return $extensions;

app/code/Magento/QuoteGraphQl/Plugin/ExceptionMessageDecorator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public function __construct(readonly ErrorMapper $errorDecorator)
2020
}
2121

2222
/**
23+
* Add error id to an exception if it is not set
24+
*
2325
* @param AggregateExceptionMessageFormatter $subject
2426
* @param mixed $result
2527
* @return mixed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ public function testPlaceOrderWithNoShippingAddress()
188188
$exceptionData = $exception->getResponseData();
189189
self::assertEquals(1, count($exceptionData['errors']));
190190
self::assertEquals(
191-
'Unable to place order: Some addresses can\'t be used due to the configurations for specific countries.',
191+
'Unable to place order: Some addresses can\'t be used' .
192+
' due to the configurations for specific countries.',
192193
$exceptionData['errors'][0]['message']
193194
);
194195
self::assertEquals(

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

Lines changed: 120 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424
use Magento\Quote\Test\Fixture\CustomerCart;
2525
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
2626
use Magento\Quote\Test\Fixture\QuoteIdMask;
27+
use Magento\QuoteGraphQl\Model\ErrorMapper;
2728
use Magento\Sales\Api\OrderRepositoryInterface;
2829
use Magento\Sales\Model\OrderFactory;
2930
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
3031
use Magento\TestFramework\Fixture\Config;
3132
use Magento\TestFramework\Fixture\DataFixture;
3233
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
3334
use Magento\TestFramework\Helper\Bootstrap;
35+
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
3436
use Magento\TestFramework\TestCase\GraphQlAbstract;
3537

3638
/**
@@ -151,7 +153,6 @@ public function testPlaceOrderWithAutoGroup()
151153
self::assertArrayHasKey('number', $response['placeOrder']['orderV2']);
152154
self::assertEquals($reservedOrderId, $response['placeOrder']['order']['order_number']);
153155
self::assertEquals($reservedOrderId, $response['placeOrder']['orderV2']['number']);
154-
self::assertEmpty(count($response['placeOrder']['errors']));
155156
$orderIncrementId = $response['placeOrder']['order']['order_number'];
156157
$order = $this->orderFactory->create();
157158
$order->loadByIncrementId($orderIncrementId);
@@ -196,13 +197,20 @@ public function testPlaceOrderWithNoEmail()
196197
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
197198
$query = $this->getQuery($maskedQuoteId);
198199

199-
$response = $this->graphQlMutation($query);
200-
self::assertEquals(1, count($response['placeOrder']['errors']));
201-
self::assertEquals('GUEST_EMAIL_MISSING', $response['placeOrder']['errors'][0]['code']);
202-
self::assertEquals(
203-
'Guest email for cart is missing.',
204-
$response['placeOrder']['errors'][0]['message']
205-
);
200+
try {
201+
$this->graphQlMutation($query);
202+
} catch (ResponseContainsErrorsException $exception) {
203+
$exceptionData = $exception->getResponseData();
204+
self::assertEquals(1, count($exceptionData['errors']));
205+
self::assertEquals(
206+
'Guest email for cart is missing.',
207+
$exceptionData['errors'][0]['message']
208+
);
209+
self::assertEquals(
210+
ErrorMapper::ERROR_GUEST_EMAIL_MISSING,
211+
$exceptionData['errors'][0]['extensions']['error_code']
212+
);
213+
}
206214
}
207215

208216
#[
@@ -214,13 +222,21 @@ public function testPlaceOrderWithNoItemsInCart()
214222
$cart = DataFixtureStorageManager::getStorage()->get('cart');
215223
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
216224
$query = $this->getQuery($maskedQuoteId);
217-
$response = $this->graphQlMutation($query);
218-
self::assertEquals(1, count($response['placeOrder']['errors']));
219-
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
220-
self::assertEquals(
221-
'A server error stopped your order from being placed. Please try to place your order again.',
222-
$response['placeOrder']['errors'][0]['message']
223-
);
225+
try {
226+
$this->graphQlMutation($query);
227+
} catch (ResponseContainsErrorsException $exception) {
228+
$exceptionData = $exception->getResponseData();
229+
self::assertEquals(1, count($exceptionData['errors']));
230+
self::assertEquals(
231+
'Unable to place order: A server error stopped your order from being placed.' .
232+
' Please try to place your order again',
233+
$exceptionData['errors'][0]['message']
234+
);
235+
self::assertEquals(
236+
ErrorMapper::ERROR_UNABLE_TO_PLACE_ORDER,
237+
$exceptionData['errors'][0]['extensions']['error_code']
238+
);
239+
}
224240
}
225241

226242
#[
@@ -235,13 +251,21 @@ public function testPlaceOrderWithNoShippingAddress()
235251
$cart = DataFixtureStorageManager::getStorage()->get('cart');
236252
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
237253
$query = $this->getQuery($maskedQuoteId);
238-
$response = $this->graphQlMutation($query);
239-
self::assertEquals(1, count($response['placeOrder']['errors']));
240-
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
241-
self::assertEquals(
242-
'Some addresses can\'t be used due to the configurations for specific countries.',
243-
$response['placeOrder']['errors'][0]['message']
244-
);
254+
try {
255+
$this->graphQlMutation($query);
256+
} catch (ResponseContainsErrorsException $exception) {
257+
$exceptionData = $exception->getResponseData();
258+
self::assertEquals(1, count($exceptionData['errors']));
259+
self::assertEquals(
260+
'Unable to place order: Some addresses can\'t be used due to the' .
261+
' configurations for specific countries.',
262+
$exceptionData['errors'][0]['message']
263+
);
264+
self::assertEquals(
265+
ErrorMapper::ERROR_UNABLE_TO_PLACE_ORDER,
266+
$exceptionData['errors'][0]['extensions']['error_code']
267+
);
268+
}
245269
}
246270

247271
#[
@@ -257,13 +281,20 @@ public function testPlaceOrderWithNoShippingMethod()
257281
$cart = DataFixtureStorageManager::getStorage()->get('cart');
258282
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
259283
$query = $this->getQuery($maskedQuoteId);
260-
$response = $this->graphQlMutation($query);
261-
self::assertEquals(1, count($response['placeOrder']['errors']));
262-
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
263-
self::assertEquals(
264-
'The shipping method is missing. Select the shipping method and try again.',
265-
$response['placeOrder']['errors'][0]['message']
266-
);
284+
try {
285+
$this->graphQlMutation($query);
286+
} catch (ResponseContainsErrorsException $exception) {
287+
$exceptionData = $exception->getResponseData();
288+
self::assertEquals(1, count($exceptionData['errors']));
289+
self::assertEquals(
290+
'Unable to place order: The shipping method is missing. Select the shipping method and try again.',
291+
$exceptionData['errors'][0]['message']
292+
);
293+
self::assertEquals(
294+
ErrorMapper::ERROR_UNABLE_TO_PLACE_ORDER,
295+
$exceptionData['errors'][0]['extensions']['error_code']
296+
);
297+
}
267298
}
268299

269300
#[
@@ -284,13 +315,24 @@ public function testPlaceOrderWithNoBillingAddress()
284315
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
285316
$query = $this->getQuery($maskedQuoteId);
286317

287-
$response = $this->graphQlMutation($query);
288-
self::assertEquals(1, count($response['placeOrder']['errors']));
289-
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
290-
self::assertStringContainsString(
291-
'Please check the billing address information.',
292-
$response['placeOrder']['errors'][0]['message']
293-
);
318+
try {
319+
$this->graphQlMutation($query);
320+
} catch (ResponseContainsErrorsException $exception) {
321+
$exceptionData = $exception->getResponseData();
322+
self::assertEquals(1, count($exceptionData['errors']));
323+
self::assertEquals(
324+
'Unable to place order: Please check the billing address information. ' .
325+
'"firstname" is required. Enter and try again. "lastname" is required. Enter and try again. ' .
326+
'"street" is required. Enter and try again. "city" is required. Enter and try again. ' .
327+
'"telephone" is required. Enter and try again. "postcode" is required. Enter and try again. ' .
328+
'"countryId" is required. Enter and try again.',
329+
$exceptionData['errors'][0]['message']
330+
);
331+
self::assertEquals(
332+
ErrorMapper::ERROR_UNABLE_TO_PLACE_ORDER,
333+
$exceptionData['errors'][0]['extensions']['error_code']
334+
);
335+
}
294336
}
295337

296338
#[
@@ -311,13 +353,20 @@ public function testPlaceOrderWithNoPaymentMethod()
311353
$cart = DataFixtureStorageManager::getStorage()->get('cart');
312354
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
313355
$query = $this->getQuery($maskedQuoteId);
314-
$response = $this->graphQlMutation($query);
315-
self::assertEquals(1, count($response['placeOrder']['errors']));
316-
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
317-
self::assertEquals(
318-
'Enter a valid payment method and try again.',
319-
$response['placeOrder']['errors'][0]['message']
320-
);
356+
try {
357+
$this->graphQlMutation($query);
358+
} catch (ResponseContainsErrorsException $exception) {
359+
$exceptionData = $exception->getResponseData();
360+
self::assertEquals(1, count($exceptionData['errors']));
361+
self::assertEquals(
362+
'Unable to place order: Enter a valid payment method and try again.',
363+
$exceptionData['errors'][0]['message']
364+
);
365+
self::assertEquals(
366+
ErrorMapper::ERROR_UNABLE_TO_PLACE_ORDER,
367+
$exceptionData['errors'][0]['extensions']['error_code']
368+
);
369+
}
321370
}
322371

323372
#[
@@ -348,13 +397,20 @@ public function testPlaceOrderWithOutOfStockProduct()
348397
$cart = DataFixtureStorageManager::getStorage()->get('cart');
349398
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
350399
$query = $this->getQuery($maskedQuoteId);
351-
$response = $this->graphQlMutation($query);
352-
self::assertEquals(1, count($response['placeOrder']['errors']));
353-
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
354-
self::assertEquals(
355-
'Some of the products are out of stock.',
356-
$response['placeOrder']['errors'][0]['message']
357-
);
400+
try {
401+
$this->graphQlMutation($query);
402+
} catch (ResponseContainsErrorsException $exception) {
403+
$exceptionData = $exception->getResponseData();
404+
self::assertEquals(1, count($exceptionData['errors']));
405+
self::assertEquals(
406+
'Unable to place order: Some of the products are out of stock.',
407+
$exceptionData['errors'][0]['message']
408+
);
409+
self::assertEquals(
410+
ErrorMapper::ERROR_UNABLE_TO_PLACE_ORDER,
411+
$exceptionData['errors'][0]['extensions']['error_code']
412+
);
413+
}
358414
}
359415

360416
#[
@@ -385,13 +441,20 @@ public function testPlaceOrderWithOutOfStockProductWithDisabledInventoryCheck()
385441
$cart = DataFixtureStorageManager::getStorage()->get('cart');
386442
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
387443
$query = $this->getQuery($maskedQuoteId);
388-
$response = $this->graphQlMutation($query);
389-
self::assertEquals(1, count($response['placeOrder']['errors']));
390-
self::assertEquals('UNABLE_TO_PLACE_ORDER', $response['placeOrder']['errors'][0]['code']);
391-
self::assertEquals(
392-
'Enter a valid payment method and try again.',
393-
$response['placeOrder']['errors'][0]['message']
394-
);
444+
try {
445+
$this->graphQlMutation($query);
446+
} catch (ResponseContainsErrorsException $exception) {
447+
$exceptionData = $exception->getResponseData();
448+
self::assertEquals(1, count($exceptionData['errors']));
449+
self::assertEquals(
450+
'Unable to place order: Enter a valid payment method and try again.',
451+
$exceptionData['errors'][0]['message']
452+
);
453+
self::assertEquals(
454+
ErrorMapper::ERROR_UNABLE_TO_PLACE_ORDER,
455+
$exceptionData['errors'][0]['extensions']['error_code']
456+
);
457+
}
395458
}
396459

397460
#[

dev/tests/integration/testsuite/Magento/PaypalGraphQl/Model/Resolver/Guest/PlaceOrderWithHostedProTest.php

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

dev/tests/integration/testsuite/Magento/PaypalGraphQl/Model/Resolver/Guest/PlaceOrderWithPayflowLinkTest.php

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

dev/tests/integration/testsuite/Magento/PaypalGraphQl/Model/Resolver/Guest/PlaceOrderWithPaymentsAdvancedTest.php

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

@@ -229,9 +229,9 @@ public function testResolveWithPaymentAdvancedDeclined(): void
229229
$this->gateway->method('postRequest')->willThrowException($exception);
230230

231231
$responseData = $this->setPaymentMethodAndPlaceOrder($cartId, $paymentMethod);
232-
$this->assertArrayHasKey('errors', $responseData['data']['placeOrder']);
233-
$actualError = $responseData['data']['placeOrder']['errors'][0];
234-
$this->assertEquals($expectedErrorCode, $actualError['code']);
232+
$this->assertArrayHasKey('errors', $responseData);
233+
$actualError = $responseData['errors'][0];
234+
$this->assertEquals($expectedErrorCode, $actualError['extensions']['error_code']);
235235
}
236236

237237
/**

0 commit comments

Comments
 (0)