Add errors per field in Response data

[MAILPOET-4700]
This commit is contained in:
David Remer
2022-10-21 10:30:47 +03:00
committed by Jan Jakeš
parent 5754302154
commit 686f702ec9
2 changed files with 17 additions and 3 deletions

View File

@@ -7,13 +7,14 @@ use MailPoet\Automation\Engine\Exceptions;
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException; use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNode; use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNode;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNodeVisitor; use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNodeVisitor;
use MailPoet\Validator\ValidationException;
use Throwable; use Throwable;
class ValidStepRule implements WorkflowNodeVisitor { class ValidStepRule implements WorkflowNodeVisitor {
/** @var WorkflowNodeVisitor[] */ /** @var WorkflowNodeVisitor[] */
private $rules; private $rules;
/** @var array<string, array{step_id: string, message: string}> */ /** @var array<string, array{step_id: string, fields: array<string,string>}> */
private $errors = []; private $errors = [];
/** @param WorkflowNodeVisitor[] $rules */ /** @param WorkflowNodeVisitor[] $rules */
@@ -45,9 +46,19 @@ class ValidStepRule implements WorkflowNodeVisitor {
try { try {
$rule->visitNode($workflow, $node); $rule->visitNode($workflow, $node);
} catch (UnexpectedValueException $e) { } catch (UnexpectedValueException $e) {
$this->errors[$stepId] = ['step_id' => $stepId, 'message' => $e->getMessage()]; if (!isset($this->errors[$stepId])) {
$this->errors[$stepId] = ['step_id' => $stepId, 'message' => $e->getMessage(), 'fields' => []];
}
$this->errors[$stepId]['fields'] = array_merge($e->getErrors(), $this->errors[$stepId]['fields']);
} catch (ValidationException $e) {
if (!isset($this->errors[$stepId])) {
$this->errors[$stepId] = ['step_id' => $stepId, 'message' => $e->getMessage(), 'fields' => []];
}
$this->errors[$stepId]['fields'] = array_merge($e->getErrors(), $this->errors[$stepId]['fields']);
} catch (Throwable $e) { } catch (Throwable $e) {
$this->errors[$stepId] = ['step_id' => $stepId, 'message' => __('Unknown error.', 'mailpoet')]; if (!isset($this->errors[$stepId])) {
$this->errors[$stepId] = ['step_id' => $stepId, 'message' => __('Unknown error.', 'mailpoet'), 'fields' => []];
}
} }
} }
} }

View File

@@ -42,11 +42,13 @@ class ValidStepRuleTest extends WorkflowRuleTest {
try { try {
(new WorkflowWalker())->walk($workflow, [$rule]); (new WorkflowWalker())->walk($workflow, [$rule]);
} catch (UnexpectedValueException $e) { } catch (UnexpectedValueException $e) {
$errors = $e->getErrors();
$this->assertSame( $this->assertSame(
[ [
'root' => [ 'root' => [
'step_id' => 'root', 'step_id' => 'root',
'message' => 'Test error', 'message' => 'Test error',
'fields' => []
], ],
], ],
$e->getErrors() $e->getErrors()
@@ -77,6 +79,7 @@ class ValidStepRuleTest extends WorkflowRuleTest {
'root' => [ 'root' => [
'step_id' => 'root', 'step_id' => 'root',
'message' => 'Unknown error.', 'message' => 'Unknown error.',
'fields' => [],
], ],
], ],
$e->getErrors() $e->getErrors()