From 98c174e7b4c430627c1456392dcda907b8d4228f Mon Sep 17 00:00:00 2001 From: Gytautas Date: Wed, 26 Jun 2024 14:20:01 +0300 Subject: [PATCH] PIPRES-445: HTTP status handling improvements --- .php-cs-fixer.dist.php | 15 ------------ Makefile | 3 +-- changelog.md | 3 +++ controllers/front/webhook.php | 44 ++++++++++++++++++++++------------- 4 files changed, 32 insertions(+), 33 deletions(-) delete mode 100644 .php-cs-fixer.dist.php diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index 8d68a2263..000000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,15 +0,0 @@ -setUsingCache(true) - ->getFinder() - ->in(__DIR__) - ->exclude('translations') - ->exclude('mails') - ->exclude('libraries') - ->exclude('vendor') - ->exclude('upgrade'); - -return $config; diff --git a/Makefile b/Makefile index 7c3ace459..194247360 100755 --- a/Makefile +++ b/Makefile @@ -90,8 +90,7 @@ npm-package-install: prepare-zip: composer install --no-dev --optimize-autoloader --classmap-authoritative composer dump-autoload --no-dev --optimize --classmap-authoritative - cp .github/.htaccess vendor/.htaccess - rm -rf .git .docker .editorconfig .github tests .php-cs-fixer.php Makefile cypress .docker cypress.config.js cypress.env.json docker-compose*.yml .gitignore bin codeception.yml package-lock.json package.json .php_cs.dist .php-cs-fixer.dist + rm -rf .git .docker .editorconfig .github tests .php-cs-fixer.php Makefile cypress .docker cypress.config.js cypress.env.json docker-compose*.yml .gitignore bin codeception.yml package-lock.json package.json .php_cs.dist .php-cs-fixer.dist .php-cs-fixer.dist.php diff --git a/changelog.md b/changelog.md index 09a8211bc..24953f1bd 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,9 @@ # Changelog # +## Changes in release 6.2.2 ## ++ Error handling improvements + ## Changes in release 6.2.1 ## + Ideal v2 payment method improvement diff --git a/controllers/front/webhook.php b/controllers/front/webhook.php index fa2710aa3..6b245fe23 100644 --- a/controllers/front/webhook.php +++ b/controllers/front/webhook.php @@ -11,8 +11,10 @@ */ use Mollie\Adapter\ToolsAdapter; +use Mollie\Api\Exceptions\ApiException; use Mollie\Controller\AbstractMollieController; use Mollie\Errors\Http\HttpStatusCode; +use Mollie\Exception\TransactionException; use Mollie\Handler\ErrorHandler\ErrorHandler; use Mollie\Infrastructure\Response\JsonResponse; use Mollie\Logger\PrestaLoggerInterface; @@ -50,9 +52,6 @@ public function initContent(): void /** @var PrestaLoggerInterface $logger */ $logger = $this->module->getService(PrestaLoggerInterface::class); - /** @var ErrorHandler $errorHandler */ - $errorHandler = $this->module->getService(ErrorHandler::class); - /** @var ToolsAdapter $tools */ $tools = $this->module->getService(ToolsAdapter::class); @@ -95,20 +94,12 @@ public function initContent(): void try { $this->executeWebhook($transactionId); + } catch (ApiException $exception) { + $this->handleException($exception, HttpStatusCode::HTTP_BAD_REQUEST, 'Api request failed'); + } catch (TransactionException $exception) { + $this->handleException($exception, $exception->getCode(), 'Failed to handle transaction'); } catch (\Throwable $exception) { - $logger->error('Failed to handle webhook', [ - 'Exception message' => $exception->getMessage(), - 'Exception code' => $exception->getCode(), - ]); - - $errorHandler->handle($exception, $exception->getCode(), false); - - $this->releaseLock(); - - $this->ajaxResponse(JsonResponse::error( - $this->module->l('Failed to handle webhook', self::FILE_NAME), - $exception->getCode() - )); + $this->handleException($exception, HttpStatusCode::HTTP_BAD_REQUEST, 'Failed to handle webhook'); } $this->releaseLock(); @@ -158,4 +149,25 @@ private function setContext(int $cartId): void $this->context->cart = $cart; } + + private function handleException(\Throwable $exception, int $httpStatusCode, string $logMessage): void + { + /** @var PrestaLoggerInterface $logger */ + $logger = $this->module->getService(PrestaLoggerInterface::class); + + /** @var ErrorHandler $errorHandler */ + $errorHandler = $this->module->getService(ErrorHandler::class); + + $logger->error($logMessage, [ + 'Exception message' => $exception->getMessage(), + 'Exception code' => $httpStatusCode + ]); + + $errorHandler->handle($exception, $httpStatusCode, false); + $this->releaseLock(); + $this->ajaxResponse(JsonResponse::error( + $this->module->l('Failed to handle webhook', self::FILE_NAME), + $httpStatusCode + )); + } }