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\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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user