Skip to content

Commit af251eb

Browse files
committed
MC-26683: Some sort of example how I see the solution of this.
1 parent 0a79989 commit af251eb

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

app/code/Magento/GraphQl/Controller/GraphQl.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ public function dispatch(RequestInterface $request) : ResponseInterface
160160
} catch (\Exception $error) {
161161
$result['errors'] = isset($result) && isset($result['errors']) ? $result['errors'] : [];
162162
$result['errors'][] = $this->graphQlError->create($error);
163+
// here we should have data from GraphQlCartInputException
164+
$result['data'] = $error->getData();
163165
$statusCode = ExceptionFormatter::HTTP_GRAPH_QL_SCHEMA_ERROR_STATUS;
164166
}
165167

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
namespace Magento\QuoteGraphQl\Model\Cart;
99

1010
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11+
use Magento\Framework\Message\MessageInterface;
1112
use Magento\Quote\Api\CartRepositoryInterface;
1213
use Magento\Quote\Model\Quote;
1314

1415
/**
15-
* Adding products to cart using GraphQL
16+
* Add products to cart
1617
*/
1718
class AddProductsToCart
1819
{
@@ -43,16 +44,29 @@ public function __construct(
4344
*
4445
* @param Quote $cart
4546
* @param array $cartItems
47+
* @return \Magento\Framework\GraphQl\Exception\GraphQlCartInputException
4648
* @throws GraphQlInputException
4749
* @throws \Magento\Framework\Exception\LocalizedException
4850
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
4951
*/
50-
public function execute(Quote $cart, array $cartItems): void
52+
public function execute(Quote $cart, array $cartItems): \Magento\Framework\GraphQl\Exception\GraphQlCartInputException
5153
{
5254
foreach ($cartItems as $cartItemData) {
5355
$this->addProductToCart->execute($cart, $cartItemData);
5456
}
5557

58+
if ($cart->getData('has_error')) {
59+
$e = new \Magento\Framework\GraphQl\Exception\GraphQlCartInputException(__('Shopping cart errors'));
60+
$errors = $cart->getErrors();
61+
foreach ($errors as $error) {
62+
/** @var MessageInterface $error */
63+
$e->addError(new GraphQlInputException(__($error->getText())));
64+
}
65+
$e->addData($cartItems);
66+
67+
throw $e;
68+
}
69+
5670
$this->cartRepository->save($cart);
5771
}
5872
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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\Framework\GraphQl\Exception;
9+
10+
use Magento\Framework\Exception\AggregateExceptionInterface;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Phrase;
13+
use GraphQL\Error\ClientAware;
14+
15+
class GraphQlCartInputException extends LocalizedException implements AggregateExceptionInterface, ClientAware
16+
{
17+
const EXCEPTION_CATEGORY = 'graphql-input';
18+
19+
/**
20+
* @var boolean
21+
*/
22+
private $isSafe;
23+
24+
/**
25+
* The array of errors that have been added via the addError() method
26+
*
27+
* @var \Magento\Framework\Exception\LocalizedException[]
28+
*/
29+
private $errors = [];
30+
31+
/**
32+
* @var array
33+
*/
34+
private $data = [];
35+
36+
/**
37+
* Initialize object
38+
*
39+
* @param Phrase $phrase
40+
* @param \Exception $cause
41+
* @param int $code
42+
* @param boolean $isSafe
43+
*/
44+
public function __construct(Phrase $phrase, \Exception $cause = null, $code = 0, $isSafe = true)
45+
{
46+
$this->isSafe = $isSafe;
47+
parent::__construct($phrase, $cause, $code);
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
public function isClientSafe() : bool
54+
{
55+
return $this->isSafe;
56+
}
57+
58+
/**
59+
* @inheritdoc
60+
*/
61+
public function getCategory() : string
62+
{
63+
return self::EXCEPTION_CATEGORY;
64+
}
65+
66+
/**
67+
* Add child error if used as aggregate exception
68+
*
69+
* @param LocalizedException $exception
70+
* @return $this
71+
*/
72+
public function addError(LocalizedException $exception): self
73+
{
74+
$this->errors[] = $exception;
75+
return $this;
76+
}
77+
78+
/**
79+
* Get child errors if used as aggregate exception
80+
*
81+
* @return LocalizedException[]
82+
*/
83+
public function getErrors(): array
84+
{
85+
return $this->errors;
86+
}
87+
88+
/**
89+
* @param array $data
90+
* @return GraphQlInputException
91+
*/
92+
public function addData(array $data): self
93+
{
94+
$this->data = $data;
95+
return $this;
96+
}
97+
}

0 commit comments

Comments
 (0)