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\Validation\WorkflowGraph\WorkflowNode;
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNodeVisitor;
use MailPoet\Validator\ValidationException;
use Throwable;
class ValidStepRule implements WorkflowNodeVisitor {
/** @var WorkflowNodeVisitor[] */
private $rules;
/** @var array<string, array{step_id: string, message: string}> */
/** @var array<string, array{step_id: string, fields: array<string,string>}> */
private $errors = [];
/** @param WorkflowNodeVisitor[] $rules */
@@ -45,9 +46,19 @@ class ValidStepRule implements WorkflowNodeVisitor {
try {
$rule->visitNode($workflow, $node);
} 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) {
$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 {
(new WorkflowWalker())->walk($workflow, [$rule]);
} catch (UnexpectedValueException $e) {
$errors = $e->getErrors();
$this->assertSame(
[
'root' => [
'step_id' => 'root',
'message' => 'Test error',
'fields' => []
],
],
$e->getErrors()
@@ -77,6 +79,7 @@ class ValidStepRuleTest extends WorkflowRuleTest {
'root' => [
'step_id' => 'root',
'message' => 'Unknown error.',
'fields' => [],
],
],
$e->getErrors()