The example shows what happens when a tasks throws an exception. Check the failed task node on the graph.
mkdir ~/pvm-exception
cd ~/pvm-exception
composer req formapro/pvm:0.4.x-dev makasim/values:0.5.x-dev
# create such file with the code below
php exception.php
exception.php
foo_task
bar_task
LogicException is caught. Message Something went wrong
exception.php
<?php
use Formapro\Pvm\DefaultBehaviorRegistry;
use Formapro\Pvm\ProcessEngine;
use Formapro\Pvm\Token;
use Formapro\Pvm\ProcessBuilder;
require_once __DIR__.'/vendor/autoload.php';
$process = (new ProcessBuilder())
->createNode('foo_task', 'print_label')->end()
->createNode('bar_task', 'throw_exception')->end()
->createNode('baz_task', 'print_label')->end()
->createTransition('foo_task', 'bar_task')->end()
->createTransition('bar_task', 'baz_task')->end()
->createStartTransition('foo_task')->end()
->getProcess()
;
$engine = new ProcessEngine(new DefaultBehaviorRegistry([
'print_label' => function(Token $token) {
echo $token->getTo()->getId().PHP_EOL;
},
'throw_exception' => function(Token $token) {
echo $token->getTo()->getId().PHP_EOL;
throw new \LogicException('Something went wrong');
},
]));
$engine->setLogException(true);
$token = $engine->createTokenFor($process->getStartTransition());
try {
$engine->proceed($token);
} catch (\Throwable $e) {
echo get_class($e).' is caught. Message '.$e->getMessage().PHP_EOL;
}
{ "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Process.json", "id": "fd188fad-6cc8-48ae-b30d-712246c460a2", "nodes": { "foo_task": { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json", "id": "foo_task", "behavior": "print_label" }, "bar_task": { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json", "id": "bar_task", "behavior": "throw_exception" }, "baz_task": { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Node.json", "id": "baz_task", "behavior": "print_label" } }, "transitions": { "60a8007d-71b3-44b1-b10a-e97f0ccd34e0": { "id": "60a8007d-71b3-44b1-b10a-e97f0ccd34e0", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "from": "foo_task", "to": "bar_task" }, "81c4cdde-6c33-44f0-873a-c99ce2b50c41": { "id": "81c4cdde-6c33-44f0-873a-c99ce2b50c41", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "from": "bar_task", "to": "baz_task" }, "3118f5a4-6915-4693-a4d2-977292b47151": { "id": "3118f5a4-6915-4693-a4d2-977292b47151", "weight": 1, "async": false, "active": true, "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json", "to": "foo_task" } }, "outTransitions": { "foo_task": [ "60a8007d-71b3-44b1-b10a-e97f0ccd34e0" ], "bar_task": [ "81c4cdde-6c33-44f0-873a-c99ce2b50c41" ] }, "inTransitions": { "bar_task": [ "60a8007d-71b3-44b1-b10a-e97f0ccd34e0" ], "baz_task": [ "81c4cdde-6c33-44f0-873a-c99ce2b50c41" ], "foo_task": [ "3118f5a4-6915-4693-a4d2-977292b47151" ] }, "tokens": { "d494cc5a-bf0d-432d-80ba-5cf3700388c6": { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json", "id": "d494cc5a-bf0d-432d-80ba-5cf3700388c6", "transitions": [ { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "4fc02486-6fb3-40b8-87e9-e352ea48e724", "transitionId": "3118f5a4-6915-4693-a4d2-977292b47151", "weight": 1, "state": "opened", "time": 16107294501165 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "823626e4-8e03-450f-a4fb-6d0dba43743c", "transitionId": "3118f5a4-6915-4693-a4d2-977292b47151", "weight": 1, "state": "passed", "time": 16107294501170 }, { "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json", "id": "2250e8c6-5481-4c9a-90e3-cbca329c844f", "transitionId": "60a8007d-71b3-44b1-b10a-e97f0ccd34e0", "weight": 1, "state": "opened", "time": 16107294501173, "exception": "LogicException: Something went wrong in \/app\/examples\/7-exception\/exception.php:26\nStack trace:\n#0 [internal function]: App\\Controller\\DemoController->{closure}(Object(Formapro\\Pvm\\Token))\n#1 \/app\/vendor\/formapro\/pvm\/src\/CallbackBehavior.php(31): call_user_func(Object(Closure), Object(Formapro\\Pvm\\Token))\n#2 \/app\/vendor\/formapro\/pvm\/src\/ProcessEngine.php(125): Formapro\\Pvm\\CallbackBehavior->execute(Object(Formapro\\Pvm\\Token))\n#3 \/app\/vendor\/formapro\/pvm\/src\/ProcessEngine.php(251): Formapro\\Pvm\\ProcessEngine->doProceed(Object(Formapro\\Pvm\\Token))\n#4 \/app\/vendor\/formapro\/pvm\/src\/ProcessEngine.php(188): Formapro\\Pvm\\ProcessEngine->transition(Object(Formapro\\Pvm\\Token))\n#5 \/app\/vendor\/formapro\/pvm\/src\/ProcessEngine.php(78): Formapro\\Pvm\\ProcessEngine->doProceed(Object(Formapro\\Pvm\\Token))\n#6 \/app\/examples\/7-exception\/exception.php(35): Formapro\\Pvm\\ProcessEngine->proceed(Object(Formapro\\Pvm\\Token))\n#7 \/app\/src\/Controller\/DemoController.php(36): require_once('\/app\/examples\/7...')\n#8 \/app\/vendor\/symfony\/http-kernel\/HttpKernel.php(149): App\\Controller\\DemoController->__invoke('exception', Object(Twig_Environment), Object(App\\Service\\GetExamplesService), Object(App\\Service\\CleanOldFileProcessesService))\n#9 \/app\/vendor\/symfony\/http-kernel\/HttpKernel.php(66): Symfony\\Component\\HttpKernel\\HttpKernel->handleRaw(Object(Symfony\\Component\\HttpFoundation\\Request), 1)\n#10 \/app\/vendor\/symfony\/http-kernel\/Kernel.php(188): Symfony\\Component\\HttpKernel\\HttpKernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request), 1, true)\n#11 \/app\/public\/index.php(37): Symfony\\Component\\HttpKernel\\Kernel->handle(Object(Symfony\\Component\\HttpFoundation\\Request))\n#12 {main}" } ] } } }