Skip to content

Commit

Permalink
add some error handling to prevent 500 error in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
revoltek-daniel committed May 21, 2024
1 parent 47d9fc1 commit 0abf4a5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions config/services/clients.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
service(SaferpayClientBodyFactoryInterface::class),
service(SaferpayApiBaseUrlResolverInterface::class),
service(PaymentEventDispatcherInterface::class),
service('monolog.logger.saferpay'),
])
;

Expand Down
32 changes: 26 additions & 6 deletions src/Client/SaferpayClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
use CommerceWeavers\SyliusSaferpayPlugin\Resolver\SaferpayApiBaseUrlResolverInterface;
use Payum\Core\Model\GatewayConfigInterface;
use Payum\Core\Security\TokenInterface;
use Psr\Log\LoggerInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TimeoutExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Webmozart\Assert\Assert;
Expand All @@ -38,6 +40,7 @@ public function __construct(
private SaferpayClientBodyFactoryInterface $saferpayClientBodyFactory,
private SaferpayApiBaseUrlResolverInterface $saferpayApiBaseUrlResolver,
private PaymentEventDispatcherInterface $paymentEventDispatcher,
private LoggerInterface $logger,
) {
}

Expand Down Expand Up @@ -262,12 +265,29 @@ private function request(
GatewayConfigInterface $gatewayConfig,
array $headers = [],
): array {
$response = $this->client->request($method, $this->provideFullUrl($gatewayConfig, $url), [
'headers' => array_merge($this->provideHeaders($gatewayConfig), $headers),
'body' => json_encode($body),
]);

$headers = $response->getHeaders(false);
$reqUrl = $this->provideFullUrl($gatewayConfig, $url);
$reqHeaders = array_merge($this->provideHeaders($gatewayConfig), $headers);
$reqBbody = json_encode($body);
try {
try {
$response = $this->client->request($method, $reqUrl, [
'headers' => $reqHeaders,
'body' => $reqBbody,
]);
$headers = $response->getHeaders(false);
} catch (TimeoutExceptionInterface $e) {
// retry once
$this->logger->error($e->getMessage());
$response = $this->client->request($method, $reqUrl, [
'headers' => $reqHeaders,
'body' => $reqBbody,
]);
$headers = $response->getHeaders(false);
}
} catch (TimeoutExceptionInterface|TransportExceptionInterface|ServerExceptionInterface $e) {
$this->logger->error($e->getMessage());
return ['error' => $e->getMessage(), 'StatusCode' => 500];
}

if (str_starts_with($headers['content-type'][0], 'application/json')) {
$responseBody = (array) json_decode($response->getContent(false), true);
Expand Down

0 comments on commit 0abf4a5

Please sign in to comment.