Rethrow integration log exceptions in debug mode

[MAILPOET-5568]
This commit is contained in:
Jan Jakes
2023-09-12 15:26:01 +02:00
committed by Aschepikov
parent 701984523f
commit 944b440bdf
2 changed files with 33 additions and 3 deletions

View File

@@ -32,13 +32,17 @@ class StepRunLogger {
/** @var int */
private $runNumber;
/** @var bool */
private $isWpDebug;
public function __construct(
AutomationRunLogStorage $automationRunLogStorage,
Hooks $hooks,
int $runId,
string $stepId,
string $stepType,
int $runNumber
int $runNumber,
bool $isWpDebug = WP_DEBUG
) {
$this->automationRunLogStorage = $automationRunLogStorage;
$this->hooks = $hooks;
@@ -46,6 +50,7 @@ class StepRunLogger {
$this->stepId = $stepId;
$this->stepType = $stepType;
$this->runNumber = $runNumber;
$this->isWpDebug = $isWpDebug;
}
public function logStart(): void {
@@ -106,7 +111,10 @@ class StepRunLogger {
try {
$this->hooks->doAutomationStepAfterRun($log);
} catch (Throwable $e) {
// ignore integration errors
if ($this->isWpDebug) {
throw $e;
}
// ignore integration logging errors
}
}
}

View File

@@ -141,7 +141,7 @@ class StepRunLoggerTest extends MailPoetTest {
}
public function testItCatchesAfterRunHookErrors(): void {
$logger = new StepRunLogger($this->storage, $this->hooks, 1, 'step-id', AutomationRunLog::TYPE_ACTION, 1);
$logger = new StepRunLogger($this->storage, $this->hooks, 1, 'step-id', AutomationRunLog::TYPE_ACTION, 1, false);
$logs = $this->storage->getLogsForAutomationRun(1);
$this->assertCount(0, $logs);
@@ -167,6 +167,28 @@ class StepRunLoggerTest extends MailPoetTest {
$this->assertLogData($logs[0], ['step_key' => 'step-key', 'status' => 'complete']);
}
public function testItRethrowsAfterRunHookErrorsInDebugMode(): void {
$logger = new StepRunLogger($this->storage, $this->hooks, 1, 'step-id', AutomationRunLog::TYPE_ACTION, 1, true);
$logs = $this->storage->getLogsForAutomationRun(1);
$this->assertCount(0, $logs);
$runs = 0;
$wp = $this->diContainer->get(WordPress::class);
$wp->addAction(Hooks::AUTOMATION_RUN_LOG_AFTER_STEP_RUN, function (AutomationRunLog $log) use (&$runs) {
$runs += 1;
throw new Exception('test error');
});
$logger->logStart();
$logger->logStepData(new Step('step-id', 'action', 'step-key', [], []));
$logger->logProgress();
$this->assertSame(0, $runs);
$this->expectException(Exception::class);
$this->expectExceptionMessage('test error');
$logger->logSuccess();
}
private function assertLogData(AutomationRunLog $log, array $data = []): void {
$error = isset($data['error']) ? [
'message' => $data['error']->getMessage(),