Add errors per field in Response data
[MAILPOET-4700]
This commit is contained in:
@@ -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' => []];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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()
|
||||
|
Reference in New Issue
Block a user