Skip to content

Commit

Permalink
Improvement: Do not log validation error mollie#839
Browse files Browse the repository at this point in the history
  • Loading branch information
michielgerritsen committed Jan 6, 2025
1 parent edfeaba commit 0fc0d49
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Model/Methods/Reorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Mollie\Payment\Model\Client\Payments as PaymentsApi;
use Mollie\Payment\Model\Mollie;
use Mollie\Payment\Service\Mollie\GetApiMethod;
use Mollie\Payment\Service\Mollie\LogException;
use Mollie\Payment\Service\OrderLockService;
use Mollie\Payment\Service\Mollie\MollieApiClient;
use Mollie\Payment\Service\Mollie\Timeout;
Expand Down Expand Up @@ -72,6 +73,7 @@ public function __construct(
MollieApiClient $mollieApiClient,
TransactionToOrderRepositoryInterface $transactionToOrderRepository,
GetApiMethod $getApiMethod,
LogException $logException,
RequestInterface $request,
$formBlockType,
$infoBlockType,
Expand Down Expand Up @@ -100,6 +102,7 @@ public function __construct(
$mollieApiClient,
$transactionToOrderRepository,
$getApiMethod,
$logException,
$formBlockType,
$infoBlockType,
$commandPool,
Expand Down
3 changes: 3 additions & 0 deletions Model/Methods/Voucher.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Mollie\Payment\Model\Client\Payments as PaymentsApi;
use Mollie\Payment\Model\Mollie;
use Mollie\Payment\Service\Mollie\GetApiMethod;
use Mollie\Payment\Service\Mollie\LogException;
use Mollie\Payment\Service\OrderLockService;
use Mollie\Payment\Service\Mollie\MollieApiClient;
use Mollie\Payment\Service\Mollie\Timeout;
Expand Down Expand Up @@ -68,6 +69,7 @@ public function __construct(
MollieApiClient $mollieApiClient,
TransactionToOrderRepositoryInterface $transactionToOrderRepository,
GetApiMethod $getApiMethod,
LogException $logException,
$formBlockType,
$infoBlockType,
QuoteHasMealVoucherProducts $quoteHasMealVoucherProducts,
Expand Down Expand Up @@ -96,6 +98,7 @@ public function __construct(
$mollieApiClient,
$transactionToOrderRepository,
$getApiMethod,
$logException,
$formBlockType,
$infoBlockType,
$commandPool,
Expand Down
9 changes: 8 additions & 1 deletion Model/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Mollie\Payment\Model\Client\Payments as PaymentsApi;
use Mollie\Payment\Model\Client\ProcessTransactionResponse;
use Mollie\Payment\Service\Mollie\GetApiMethod;
use Mollie\Payment\Service\Mollie\LogException;
use Mollie\Payment\Service\OrderLockService;
use Mollie\Payment\Service\Mollie\Timeout;
use Mollie\Payment\Service\Mollie\Wrapper\MollieApiClientFallbackWrapper;
Expand Down Expand Up @@ -121,6 +122,10 @@ class Mollie extends Adapter
* @var GetApiMethod
*/
private $getApiMethod;
/**
* @var LogException
*/
private $logException;

public function __construct(
ManagerInterface $eventManager,
Expand All @@ -142,6 +147,7 @@ public function __construct(
\Mollie\Payment\Service\Mollie\MollieApiClient $mollieApiClient,
TransactionToOrderRepositoryInterface $transactionToOrderRepository,
GetApiMethod $getApiMethod,
LogException $logException,
$formBlockType,
$infoBlockType,
CommandPoolInterface $commandPool = null,
Expand Down Expand Up @@ -179,6 +185,7 @@ public function __construct(
$this->mollieApiClient = $mollieApiClient;
$this->transactionToOrderRepository = $transactionToOrderRepository;
$this->getApiMethod = $getApiMethod;
$this->logException = $logException;
}

public function getCode()
Expand Down Expand Up @@ -280,7 +287,7 @@ private function startTransactionUsingTheOrdersApi(OrderInterface $order, Mollie
return $this->ordersApi->startTransaction($order, $mollieApi);
});
} catch (\Exception $exception) {
$this->mollieHelper->addTolog('error', $exception->getMessage());
$this->logException->execute($exception);
}

$methodCode = $this->mollieHelper->getMethodCode($order);
Expand Down
44 changes: 44 additions & 0 deletions Service/Mollie/LogException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Service\Mollie;

use Mollie\Payment\Config;

class LogException
{
/**
* @var Config
*/
private $config;

private $messagesToSkip = [
'The \'billingAddress.familyName\' field contains characters that are not allowed'
];

public function __construct(
Config $config,
array $messagesToSkip = []
) {
$this->config = $config;
$this->messagesToSkip = array_merge($this->messagesToSkip, $messagesToSkip);
}

public function execute(\Exception $exception): void
{
$message = method_exists($exception, 'getPlainMessage') ?
$exception->getPlainMessage() :
$exception->getMessage();

if (in_array($message, $this->messagesToSkip)) {
return;
}

$this->config->addTolog('error', $message);
}
}
31 changes: 31 additions & 0 deletions Test/Fakes/ConfigFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Test\Fakes;

use Mollie\Payment\Config;

class ConfigFake extends Config
{
private $loggedMessages = [];

public function addTolog($type, $message)
{
parent::addToLog($type, $message);

$this->loggedMessages[] = [
'type' => $type,
'message' => $message,
];
}

public function getLoggedMessages(): array
{
return $this->loggedMessages;
}
}
66 changes: 66 additions & 0 deletions Test/Integration/Service/Mollie/LogExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Test\Integration\Service\Mollie;

use Mollie\Api\Exceptions\ApiException;
use Mollie\Payment\Service\Mollie\LogException;
use Mollie\Payment\Test\Fakes\ConfigFake;
use Mollie\Payment\Test\Integration\IntegrationTestCase;

class LogExceptionTest extends IntegrationTestCase
{
public function testLogsMessages(): void
{
$exception = new ApiException('This is a test exception triggered in ' . __FILE__);

$configFake = $this->objectManager->create(ConfigFake::class);

/** @var LogException $instance */
$instance = $this->objectManager->create(LogException::class, [
'config' => $configFake,
]);
$instance->execute($exception);

$this->assertCount(1, $configFake->getLoggedMessages());
}

public function testSkipsSomeMessages(): void
{
$exception = new ApiException('The \'billingAddress.familyName\' field contains characters that are not allowed');

$configFake = $this->objectManager->create(ConfigFake::class);

/** @var LogException $instance */
$instance = $this->objectManager->create(LogException::class, [
'config' => $configFake,
]);
$instance->execute($exception);

$this->assertCount(0, $configFake->getLoggedMessages());
}

public function testCanAddOwnExceptions(): void
{
$exception = new ApiException('Some random message');

$configFake = $this->objectManager->create(ConfigFake::class);

/** @var LogException $instance */
$instance = $this->objectManager->create(LogException::class, [
// Normally you would do this through di.xml, but when testing we can do it like this.
'messagesToSkip' => [
'Some random message',
],
'config' => $configFake,
]);
$instance->execute($exception);

$this->assertCount(0, $configFake->getLoggedMessages());
}
}

0 comments on commit 0fc0d49

Please sign in to comment.