Skip to content

Commit cd82136

Browse files
committed
AC-1271: Add rate limiting for payment information endpoint and mutation
1 parent 922784d commit cd82136

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

app/code/Magento/GraphQl/Model/Backpressure/BackpressureFieldValidator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Framework\App\Backpressure\BackpressureExceededException;
1212
use Magento\Framework\App\BackpressureEnforcerInterface;
1313
use Magento\Framework\GraphQl\Config\Element\Field;
14-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1514
use Magento\Framework\GraphQl\Query\Resolver\Argument\ValidatorInterface;
1615

1716
/**
@@ -42,7 +41,10 @@ public function __construct(
4241
}
4342

4443
/**
45-
* @inheritDoc
44+
* @param Field $field
45+
* @param $args
46+
* @return void
47+
* @throws GraphQlTooManyRequestsException
4648
*/
4749
public function validate(Field $field, $args): void
4850
{
@@ -54,9 +56,7 @@ public function validate(Field $field, $args): void
5456
try {
5557
$this->backpressureEnforcer->enforce($context);
5658
} catch (BackpressureExceededException $exception) {
57-
throw new GraphQlInputException(
58-
__('Something went wrong while processing the request. Try again later')
59-
);
59+
throw new GraphQlTooManyRequestsException(__('Too Many Requests'));
6060
}
6161
}
6262
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\GraphQl\Model\Backpressure;
9+
10+
use Exception;
11+
use GraphQL\Error\ClientAware;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Phrase;
14+
15+
/**
16+
* Exception to GraphQL that is thrown when the user submits too many requests
17+
*/
18+
class GraphQlTooManyRequestsException extends LocalizedException implements ClientAware
19+
{
20+
const EXCEPTION_CATEGORY = 'graphql-too-many-requests';
21+
22+
/**
23+
* @var boolean
24+
*/
25+
private $isSafe;
26+
27+
/**
28+
* @param Phrase $phrase
29+
* @param Exception|null $cause
30+
* @param $code
31+
* @param $isSafe
32+
*/
33+
public function __construct(Phrase $phrase, Exception $cause = null, $code = 0, $isSafe = true)
34+
{
35+
$this->isSafe = $isSafe;
36+
parent::__construct($phrase, $cause, $code);
37+
}
38+
39+
/**
40+
* @inheritdoc
41+
*/
42+
public function isClientSafe(): bool
43+
{
44+
return $this->isSafe;
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function getCategory(): string
51+
{
52+
return self::EXCEPTION_CATEGORY;
53+
}
54+
}

0 commit comments

Comments
 (0)