Add logging errors to the log table
[MAILPOET-4104]
This commit is contained in:
@ -4,6 +4,7 @@ namespace MailPoet\API\JSON;
|
|||||||
|
|
||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Config\AccessControl;
|
||||||
use MailPoet\Exception;
|
use MailPoet\Exception;
|
||||||
|
use MailPoet\Logging\LoggerFactory;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
use MailPoet\Subscription\Captcha\CaptchaConstants;
|
use MailPoet\Subscription\Captcha\CaptchaConstants;
|
||||||
use MailPoet\Tracy\ApiPanel\ApiPanel;
|
use MailPoet\Tracy\ApiPanel\ApiPanel;
|
||||||
@ -42,6 +43,9 @@ class API {
|
|||||||
/** @var SettingsController */
|
/** @var SettingsController */
|
||||||
private $settings;
|
private $settings;
|
||||||
|
|
||||||
|
/** @var LoggerFactory */
|
||||||
|
private $loggerFactory;
|
||||||
|
|
||||||
const CURRENT_VERSION = 'v1';
|
const CURRENT_VERSION = 'v1';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
@ -49,6 +53,7 @@ class API {
|
|||||||
AccessControl $accessControl,
|
AccessControl $accessControl,
|
||||||
ErrorHandler $errorHandler,
|
ErrorHandler $errorHandler,
|
||||||
SettingsController $settings,
|
SettingsController $settings,
|
||||||
|
LoggerFactory $loggerFactory,
|
||||||
WPFunctions $wp
|
WPFunctions $wp
|
||||||
) {
|
) {
|
||||||
$this->container = $container;
|
$this->container = $container;
|
||||||
@ -62,6 +67,7 @@ class API {
|
|||||||
$availableApiVersion
|
$availableApiVersion
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
$this->loggerFactory = $loggerFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function init() {
|
public function init() {
|
||||||
@ -209,11 +215,13 @@ class API {
|
|||||||
$response = $endpoint->{$this->requestMethod}($this->requestData);
|
$response = $endpoint->{$this->requestMethod}($this->requestData);
|
||||||
return $response;
|
return $response;
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
$this->logError($e);
|
||||||
return $this->errorHandler->convertToResponse($e);
|
return $this->errorHandler->convertToResponse($e);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
if (class_exists(Debugger::class) && Debugger::$logDirectory) {
|
if (class_exists(Debugger::class) && Debugger::$logDirectory) {
|
||||||
Debugger::log($e, ILogger::EXCEPTION);
|
Debugger::log($e, ILogger::EXCEPTION);
|
||||||
}
|
}
|
||||||
|
$this->logError($e);
|
||||||
$errorMessage = $e->getMessage();
|
$errorMessage = $e->getMessage();
|
||||||
$errorResponse = $this->createErrorResponse(Error::BAD_REQUEST, $errorMessage, Response::STATUS_BAD_REQUEST);
|
$errorResponse = $this->createErrorResponse(Error::BAD_REQUEST, $errorMessage, Response::STATUS_BAD_REQUEST);
|
||||||
return $errorResponse;
|
return $errorResponse;
|
||||||
@ -270,4 +278,14 @@ class API {
|
|||||||
);
|
);
|
||||||
return $errorResponse;
|
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(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ use MailPoet\Config\AccessControl;
|
|||||||
use MailPoet\DI\ContainerConfigurator;
|
use MailPoet\DI\ContainerConfigurator;
|
||||||
use MailPoet\DI\ContainerFactory;
|
use MailPoet\DI\ContainerFactory;
|
||||||
use MailPoet\Entities\SubscriberSegmentEntity;
|
use MailPoet\Entities\SubscriberSegmentEntity;
|
||||||
|
use MailPoet\Logging\LoggerFactory;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
use MailPoet\WP\Functions as WPFunctions;
|
use MailPoet\WP\Functions as WPFunctions;
|
||||||
use MailPoetVendor\Symfony\Component\DependencyInjection\Container;
|
use MailPoetVendor\Symfony\Component\DependencyInjection\Container;
|
||||||
@ -38,6 +39,9 @@ class APITest extends \MailPoetTest {
|
|||||||
/** @var SettingsController */
|
/** @var SettingsController */
|
||||||
private $settings;
|
private $settings;
|
||||||
|
|
||||||
|
/** @var LoggerFactory */
|
||||||
|
private $loggerFactory;
|
||||||
|
|
||||||
public function _before() {
|
public function _before() {
|
||||||
parent::_before();
|
parent::_before();
|
||||||
// create WP user
|
// create WP user
|
||||||
@ -56,11 +60,13 @@ class APITest extends \MailPoetTest {
|
|||||||
$this->container->compile();
|
$this->container->compile();
|
||||||
$this->errorHandler = $this->container->get(ErrorHandler::class);
|
$this->errorHandler = $this->container->get(ErrorHandler::class);
|
||||||
$this->settings = $this->container->get(SettingsController::class);
|
$this->settings = $this->container->get(SettingsController::class);
|
||||||
|
$this->loggerFactory = $this->container->get(LoggerFactory::class);
|
||||||
$this->api = new JSONAPI(
|
$this->api = new JSONAPI(
|
||||||
$this->container,
|
$this->container,
|
||||||
$this->container->get(AccessControl::class),
|
$this->container->get(AccessControl::class),
|
||||||
$this->errorHandler,
|
$this->errorHandler,
|
||||||
$this->settings,
|
$this->settings,
|
||||||
|
$this->loggerFactory,
|
||||||
new WPFunctions
|
new WPFunctions
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -242,7 +248,7 @@ class APITest extends \MailPoetTest {
|
|||||||
['validatePermission' => false]
|
['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->addEndpointNamespace($namespace['name'], $namespace['version']);
|
||||||
$api->setRequestData($data, Endpoint::TYPE_POST);
|
$api->setRequestData($data, Endpoint::TYPE_POST);
|
||||||
$response = $api->processRoute();
|
$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();
|
expect($api->validatePermissions(null, $permissions))->false();
|
||||||
|
|
||||||
$accessControl = Stub::make(
|
$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();
|
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();
|
expect($api->validatePermissions('test', $permissions))->false();
|
||||||
|
|
||||||
$accessControl = Stub::make(
|
$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();
|
expect($api->validatePermissions('test', $permissions))->true();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user