Collect all step error types instead of terminating on the first one

[MAILPOET-4659]
This commit is contained in:
Jan Jakes
2022-10-06 10:02:53 +02:00
committed by Jan Jakeš
parent 04ca19296b
commit a82896e794
7 changed files with 188 additions and 58 deletions

View File

@ -3,7 +3,6 @@
namespace MailPoet\Automation\Engine\Validation\WorkflowRules;
use MailPoet\Automation\Engine\Data\Workflow;
use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Registry;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNode;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNodeVisitor;
@ -16,9 +15,6 @@ class ValidStepArgsRule implements WorkflowNodeVisitor {
/** @var Validator */
private $validator;
/** @var array<string, array{step_id: string, message: string}> */
private $errors = [];
public function __construct(
Registry $registry,
Validator $validator
@ -28,7 +24,6 @@ class ValidStepArgsRule implements WorkflowNodeVisitor {
}
public function initialize(Workflow $workflow): void {
$this->errors = [];
}
public function visitNode(Workflow $workflow, WorkflowNode $node): void {
@ -39,25 +34,11 @@ class ValidStepArgsRule implements WorkflowNodeVisitor {
}
// validate args schema only for active workflows
if ($workflow->getStatus() !== Workflow::STATUS_ACTIVE) {
return;
}
try {
if ($workflow->getStatus() === Workflow::STATUS_ACTIVE) {
$this->validator->validate($registryStep->getArgsSchema(), $step->getArgs());
} catch (Throwable $e) {
$this->errors[$step->getId()] = [
'step_id' => $step->getId(),
'message' => $e instanceof ValidationException
? $e->getWpError()->get_error_message()
: __('Unknown error.', 'mailpoet'),
];
}
}
public function complete(Workflow $workflow): void {
if ($this->errors) {
throw Exceptions::workflowNotValid(__('Some steps have invalid arguments', 'mailpoet'), $this->errors);
}
}
}