chg: [workflow:normalizeData] Gracefully cath exception and provide more feedback when supplying wrong input data

Fix #9344
pull/9370/head
Sami Mokaddem 2023-10-25 16:20:39 +02:00
parent 8d01368bf7
commit 2253338b65
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
4 changed files with 36 additions and 3 deletions

View File

@ -172,6 +172,12 @@ class WorkflowsController extends AppController
{
if ($this->request->is('post') || $this->request->is('put')) {
$blockingErrors = [];
if (!JsonTool::isValid($this->request->data['Workflow']['data'])) {
return $this->RestResponse->viewData([
'success' => false,
'outcome' => __('Invalid JSON'),
], $this->response->type());
}
$data = JsonTool::decode($this->request->data['Workflow']['data']);
$result = $this->Workflow->executeWorkflow($workflow_id, $data, $blockingErrors);
if (!empty($logging) && empty($result['success'])) {

View File

@ -522,7 +522,25 @@ class Workflow extends AppModel
$workflow = $this->__incrementWorkflowExecutionCount($workflow);
$walkResult = [];
$debugData = ['original' => $data];
$data = $this->__normalizeDataForTrigger($triggerModule, $data);
$conversionFailure = false;
try {
$data = $this->__normalizeDataForTrigger($triggerModule, $data);
} catch (Exception $e) {
$conversionFailure = true;
$message = __('Error while normalizing data for trigger. Error:' . PHP_EOL . $e->getMessage());
}
if ($data === false) {
$conversionFailure = true;
$message = __('Error while normalizing data for trigger. Invalid input.');
}
if ($conversionFailure) {
$this->logExecutionIfDebug($workflow, $message);
return [
'outcomeText' => 'failure' . sprintf(' %s', $message),
'walkResult' => [],
'success' => false,
];
}
$debugData['normalized'] = $data;
$for_path = !empty($triggerModule->blocking) ? GraphWalker::PATH_TYPE_BLOCKING : GraphWalker::PATH_TYPE_NON_BLOCKING;
$this->sendRequestToDebugEndpointIfDebug($workflow, [], '/init?type=' . $for_path, $debugData);
@ -710,10 +728,10 @@ class Workflow extends AppModel
return $success;
}
private function __normalizeDataForTrigger($triggerClass, array $data): array
private function __normalizeDataForTrigger($triggerClass, array $data)
{
if (method_exists($triggerClass, 'normalizeData')) {
return $triggerClass->normalizeData($data);
$data = $triggerClass->normalizeData($data);
}
return $data;
}

View File

@ -23,6 +23,11 @@ class Module_post_after_save extends WorkflowBaseTriggerModule
public function normalizeData(array $data)
{
parent::normalizeData($data);
if (empty($data['Post'])) {
return false;
}
$this->Thread = ClassRegistry::init('Thread');
$thread = $this->Thread->find('first', [
'recursive' => -1,

View File

@ -26,6 +26,10 @@ class Module_sighting_after_save extends WorkflowBaseTriggerModule
$this->Event = ClassRegistry::init('Event');
$this->Attribute = ClassRegistry::init('Attribute');
if (empty($data['Sighting'])) {
return false;
}
// We are missing data such as tags or objects.
$event = $this->Event->quickFetchEvent($data['Sighting']['Event']['id']);
$attribute = $this->Attribute->fetchAttribute($data['Sighting']['Attribute']['id']);