Only allow a single error per log

[MAILPOET-4463]
This commit is contained in:
John Oleksowicz
2022-09-12 13:04:52 -05:00
committed by Jan Jakeš
parent 3356bb855d
commit f126bdb2b9
5 changed files with 24 additions and 17 deletions

View File

@@ -126,13 +126,13 @@ class StepHandler {
$log->markCompletedSuccessfully();
} catch (Throwable $e) {
$log->markFailed();
$log->addError($e);
$log->setError($e);
throw $e;
} finally {
try {
$this->hooks->doWorkflowStepAfterRun($log);
} catch (Throwable $e) {
$log->addError($e);
// Ignore integration errors
}
$this->workflowRunLogStorage->createWorkflowRunLog($log);
}

View File

@@ -28,7 +28,7 @@ class WorkflowRunLog {
private $status;
/** @var array */
private $errors;
private $error;
/** @var array */
private $data;
@@ -57,7 +57,7 @@ class WorkflowRunLog {
$now = new DateTimeImmutable();
$this->createdAt = $now;
$this->errors = [];
$this->error = [];
$this->data = [];
}
@@ -81,8 +81,8 @@ class WorkflowRunLog {
return $this->args;
}
public function getErrors(): array {
return $this->errors;
public function getError(): array {
return $this->error;
}
public function getData(): array {
@@ -117,7 +117,7 @@ class WorkflowRunLog {
'created_at' => $this->createdAt->format(DateTimeImmutable::W3C),
'completed_at' => $this->completedAt ? $this->completedAt->format(DateTimeImmutable::W3C) : null,
'args' => Json::encode($this->args),
'errors' => Json::encode($this->errors),
'error' => Json::encode($this->error),
'data' => Json::encode($this->data),
];
}
@@ -132,7 +132,7 @@ class WorkflowRunLog {
$this->completedAt = new DateTimeImmutable();
}
public function addError(Throwable $error): void {
public function setError(Throwable $error): void {
$error = [
'message' => $error->getMessage(),
'errorClass' => get_class($error),
@@ -140,14 +140,14 @@ class WorkflowRunLog {
'trace' => $error->getTrace(),
];
$this->errors[] = $error;
$this->error = $error;
}
public static function fromArray(array $data): self {
$workflowRunLog = new WorkflowRunLog((int)$data['workflow_run_id'], $data['step_id'], []);
$workflowRunLog->id = (int)$data['id'];
$workflowRunLog->status = $data['status'];
$workflowRunLog->errors = Json::decode($data['errors']);
$workflowRunLog->error = Json::decode($data['error']);
$workflowRunLog->data = Json::decode($data['data']);
$workflowRunLog->args = Json::decode($data['args']);
$workflowRunLog->createdAt = new DateTimeImmutable($data['created_at']);

View File

@@ -71,7 +71,7 @@ class Migrator {
created_at timestamp NOT NULL,
completed_at timestamp NULL DEFAULT NULL,
args longtext,
errors longtext,
error longtext,
data longtext,
PRIMARY KEY (id)
);

View File

@@ -139,8 +139,7 @@ class WorkflowRunLogTest extends \MailPoetTest {
});
expect($workflowRunLogs)->count(1);
$log = $workflowRunLogs[0];
expect($log->getErrors())->count(1);
$error = $log->getErrors()[0];
$error = $log->getError();
expect($error['message'])->equals('error');
expect($error['code'])->equals(12345);
expect($error['errorClass'])->equals('Exception');

View File

@@ -25,14 +25,22 @@ class WorkflowRunLogStorageTest extends \MailPoetTest {
expect($preSave)->equals($fromDatabase->toArray());
}
public function testItStoresErrors() {
public function testItCanStoreAnError() {
$log = new WorkflowRunLog(1, 'step-id', []);
$log->addError(new \Exception('test'));
$log->setError(new \Exception('test'));
$id = $this->storage->createWorkflowRunLog($log);
$log = $this->storage->getWorkflowRunLog($id);
$this->assertInstanceOf(WorkflowRunLog::class, $log);
$errors = $log->getErrors();
expect($errors)->count(1);
$errors = $log->getError();
expect($errors)->array();
expect(array_keys($errors))->equals([
'message',
'errorClass',
'code',
'trace'
]);
expect($errors['trace'])->array();
expect(count($errors['trace']))->greaterThan(0);
}
public function _after() {