From b0ab2f404ff461f7380db794edfac93fc281e025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lys=C3=BD?= Date: Mon, 2 Jan 2023 19:05:06 +0100 Subject: [PATCH] Add logging errors to the log table [MAILPOET-4104] --- mailpoet/lib/API/JSON/API.php | 18 ++++++++++++++++++ .../tests/integration/API/JSON/APITest.php | 16 +++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/mailpoet/lib/API/JSON/API.php b/mailpoet/lib/API/JSON/API.php index 2b4087340d..9af9044542 100644 --- a/mailpoet/lib/API/JSON/API.php +++ b/mailpoet/lib/API/JSON/API.php @@ -4,6 +4,7 @@ namespace MailPoet\API\JSON; use MailPoet\Config\AccessControl; use MailPoet\Exception; +use MailPoet\Logging\LoggerFactory; use MailPoet\Settings\SettingsController; use MailPoet\Subscription\Captcha\CaptchaConstants; use MailPoet\Tracy\ApiPanel\ApiPanel; @@ -42,6 +43,9 @@ class API { /** @var SettingsController */ private $settings; + /** @var LoggerFactory */ + private $loggerFactory; + const CURRENT_VERSION = 'v1'; public function __construct( @@ -49,6 +53,7 @@ class API { AccessControl $accessControl, ErrorHandler $errorHandler, SettingsController $settings, + LoggerFactory $loggerFactory, WPFunctions $wp ) { $this->container = $container; @@ -62,6 +67,7 @@ class API { $availableApiVersion ); } + $this->loggerFactory = $loggerFactory; } public function init() { @@ -209,11 +215,13 @@ class API { $response = $endpoint->{$this->requestMethod}($this->requestData); return $response; } catch (Exception $e) { + $this->logError($e); return $this->errorHandler->convertToResponse($e); } catch (Throwable $e) { if (class_exists(Debugger::class) && Debugger::$logDirectory) { Debugger::log($e, ILogger::EXCEPTION); } + $this->logError($e); $errorMessage = $e->getMessage(); $errorResponse = $this->createErrorResponse(Error::BAD_REQUEST, $errorMessage, Response::STATUS_BAD_REQUEST); return $errorResponse; @@ -270,4 +278,14 @@ class API { ); return $errorResponse; } + + private function logError(Throwable $e): void { + $this->loggerFactory->getLogger(LoggerFactory::TOPIC_API)->error($e->getMessage(), [ + 'requestMethod' => $this->requestMethod, + 'requestData' => $this->requestData, + 'requestEndpoint' => $this->requestEndpoint, + 'exceptionMessage' => $e->getMessage(), + 'exceptionTrace' => $e->getTrace(), + ]); + } } diff --git a/mailpoet/tests/integration/API/JSON/APITest.php b/mailpoet/tests/integration/API/JSON/APITest.php index 99726f7be8..f0c88ab8d3 100644 --- a/mailpoet/tests/integration/API/JSON/APITest.php +++ b/mailpoet/tests/integration/API/JSON/APITest.php @@ -16,6 +16,7 @@ use MailPoet\Config\AccessControl; use MailPoet\DI\ContainerConfigurator; use MailPoet\DI\ContainerFactory; use MailPoet\Entities\SubscriberSegmentEntity; +use MailPoet\Logging\LoggerFactory; use MailPoet\Settings\SettingsController; use MailPoet\WP\Functions as WPFunctions; use MailPoetVendor\Symfony\Component\DependencyInjection\Container; @@ -38,6 +39,9 @@ class APITest extends \MailPoetTest { /** @var SettingsController */ private $settings; + /** @var LoggerFactory */ + private $loggerFactory; + public function _before() { parent::_before(); // create WP user @@ -56,11 +60,13 @@ class APITest extends \MailPoetTest { $this->container->compile(); $this->errorHandler = $this->container->get(ErrorHandler::class); $this->settings = $this->container->get(SettingsController::class); + $this->loggerFactory = $this->container->get(LoggerFactory::class); $this->api = new JSONAPI( $this->container, $this->container->get(AccessControl::class), $this->errorHandler, $this->settings, + $this->loggerFactory, new WPFunctions ); } @@ -242,7 +248,7 @@ class APITest extends \MailPoetTest { ['validatePermission' => false] ); - $api = new JSONAPI($this->container, $accessControl, $this->errorHandler, $this->settings, new WPFunctions); + $api = new JSONAPI($this->container, $accessControl, $this->errorHandler, $this->settings, $this->loggerFactory, new WPFunctions); $api->addEndpointNamespace($namespace['name'], $namespace['version']); $api->setRequestData($data, Endpoint::TYPE_POST); $response = $api->processRoute(); @@ -264,7 +270,7 @@ class APITest extends \MailPoetTest { ] ); - $api = new JSONAPI($this->container, $accessControl, $this->errorHandler, $this->settings, new WPFunctions); + $api = new JSONAPI($this->container, $accessControl, $this->errorHandler, $this->settings, $this->loggerFactory, new WPFunctions); expect($api->validatePermissions(null, $permissions))->false(); $accessControl = Stub::make( @@ -276,7 +282,7 @@ class APITest extends \MailPoetTest { }), ] ); - $api = new JSONAPI($this->container, $accessControl, $this->errorHandler, $this->settings, new WPFunctions); + $api = new JSONAPI($this->container, $accessControl, $this->errorHandler, $this->settings, $this->loggerFactory, new WPFunctions); expect($api->validatePermissions(null, $permissions))->true(); } @@ -298,7 +304,7 @@ class APITest extends \MailPoetTest { ] ); - $api = new JSONAPI($this->container, $accessControl, $this->errorHandler, $this->settings, new WPFunctions); + $api = new JSONAPI($this->container, $accessControl, $this->errorHandler, $this->settings, $this->loggerFactory, new WPFunctions); expect($api->validatePermissions('test', $permissions))->false(); $accessControl = Stub::make( @@ -311,7 +317,7 @@ class APITest extends \MailPoetTest { ] ); - $api = new JSONAPI($this->container, $accessControl, $this->errorHandler, $this->settings, new WPFunctions); + $api = new JSONAPI($this->container, $accessControl, $this->errorHandler, $this->settings, $this->loggerFactory, new WPFunctions); expect($api->validatePermissions('test', $permissions))->true(); }