Skip to content

Commit

Permalink
Merge pull request 5941 from hotfix/v4.3.3 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Project Collection Build Service (51degrees) authored and Project Collection Build Service (51degrees) committed Oct 1, 2021
2 parents 7be5dc2 + 3bad933 commit 9c51b8f
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 21 deletions.
26 changes: 18 additions & 8 deletions FlowData.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,17 @@ public function process()
// Set processed flag to true. FlowData can only be processed once

$this->processed = true;
return $this;

} else {
$this->setError("global", Messages::FLOW_DATA_PROCESSED);
$this->setError("global", new \Exception(Messages::FLOW_DATA_PROCESSED));
}

if (count($this->errors) != 0 && $this->pipeline->suppressProcessExceptions === false) {
$exception = reset($this->errors);
throw $exception;
}

return $this;
}

/**
Expand Down Expand Up @@ -138,17 +145,20 @@ public function setElementData($data)
/**
* Set error (should be keyed by FlowElement dataKey)
* @param string key
* @param string error message
* @param Exception error
*/
public function setError($key, $error)
{
if (!isset($this->errors[$key])) {
$this->errors[$key] = array();
}
$this->errors[$key] = $error;

$logMessage = "Error occurred during processing";

$this->pipeline->log("error", $error);
if (!empty($key)) {
$logMessage = $logMessage . " of " . $key . ". \n" . $error;
}

$this->pipeline->log("error", $logMessage);

$this->errors[$key][] = $error;
}

/**
Expand Down
7 changes: 2 additions & 5 deletions JavascriptBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

namespace fiftyone\pipeline\core;

use NodejsPhpFallback\Uglify;

/**
* The JavaScriptBuilder aggregates JavaScript properties
* from FlowElements in the Pipeline. This JavaScript also (when needed)
Expand Down Expand Up @@ -169,10 +167,9 @@ public function processInternal($flowData)

$output = $m->render(file_get_contents(__DIR__ . "/JavaScriptResource.mustache"), $vars);

if($this->minify) {
if($this->minify) {
// Minify the output
$uglify = new Uglify(array($output));
$output = $uglify->getMinifiedJs();
$output = \JShrink\Minifier::minify($output);
}

$data = new ElementDataDictionary($this, ["javascript" => $output]);
Expand Down
10 changes: 10 additions & 0 deletions Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public function __construct($flowElements, $settings)
$this->logger = $settings["logger"];
}

/* If true then pipeline will suppress exceptions
added to FlowData errors otherwise will throw the
exception occurred during the processing of first
element.*/
if (!isset($settings["suppressProcessExceptions"])) {
$this->suppressProcessExceptions = false;
} else {
$this->suppressProcessExceptions = $settings["suppressProcessExceptions"];
}

$this->log("info", "test");

$this->flowElements = $flowElements;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"type": "library",
"require": {
"mustache/mustache": "^2.13",
"nodejs-php-fallback/uglify": "^1.0"
"tedivm/jshrink": "~1.0"
},
"autoload": {
"classmap": [
Expand Down
4 changes: 0 additions & 4 deletions examples/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
$fe1 = new exampleFlowElementA();
$fe2 = new exampleFlowElementB();

// A FlowElement that causes an error
$feError = new errorFlowElement();

// A FlowElement that stops processing (and prevents and subsequent elements in the Pipeline from processing)

$feStop = new stopFlowElement();
Expand All @@ -67,7 +64,6 @@ public function logInternal($log)

$Pipeline = (new PipelineBuilder())
->add($fe1)
->add($feError)
->add($feStop)
->add($fe2)
->addLogger(new ArrayLogger("info"))
Expand Down
3 changes: 2 additions & 1 deletion tests/classes/TestPipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TestPipeline

public $logger;

public function __construct()
public function __construct($suppressException = true)
{
$this->logger = new MemoryLogger("info");
$this->flowElement1 = new ExampleFlowElement1();
Expand All @@ -32,6 +32,7 @@ public function __construct()
->add(new ExampleFlowElement2())
->addLogger($this->logger)
->build();
$this->pipeline->suppressProcessExceptions = $suppressException;
$this->flowData = $this->pipeline->createFlowData();
$this->flowData->evidence->set("header.user-agent", "test");
$this->flowData->evidence->set("some.other-evidence", "test");
Expand Down
34 changes: 33 additions & 1 deletion tests/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ public function testStopFlowData()
$this->assertTrue($getValue === null);
}

// Test exception is thrown when not suppressed.
public function testErrors_DontSuppressException() {

try {
$testPipeline = new TestPipeline(false);
$this->fail("Exception is expected.");
}
catch (\Exception $e) {
$this->assertTrue(!empty($e->getMessage()));
}
}

// Test errors are returned
public function testErrors()
{
Expand All @@ -109,12 +121,32 @@ public function testErrors()
$this->assertTrue(isset($getValue));
}

// Test Already Processed FlowData.
public function testErrors_AlreadyProcessed() {

$logger = new MemoryLogger("info");
$flowElement1 = new ExampleFlowElement1();
$pipeline = (new PipelineBuilder())
->add($flowElement1)
->addLogger($logger)
->build();
$flowData = $pipeline->createFlowData();
$flowData->process();
try {
$flowData->process();
$this->fail("Exception is expected.");
}
catch (\Exception $e) {
$this->assertEquals($e->getMessage(), "FlowData already processed");
}
}

// Test if adding properties at a later stage works (for cloud FlowElements for example)
public function testUpdateProperties()
{
$flowElement1 = new ExampleFlowElement1();
$logger = new MemoryLogger("info");
$pipeline = (new PipelineBuilder())->add($flowElement1)->add(new ErrorFlowData())
$pipeline = (new PipelineBuilder())->add($flowElement1)
->add(new StopFlowData())
->add(new ExampleFlowElement2())
->addLogger($logger)
Expand Down

0 comments on commit 9c51b8f

Please sign in to comment.