Exception

The example shows what happens when a tasks throws an exception. Check the failed task node on the graph.

Setup


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

Result

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": "341e4cbe-e058-4c0d-a634-4fdd3c8c2daf",
    "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": {
        "c6f60049-e129-43d7-9649-c6c919db24a4": {
            "id": "c6f60049-e129-43d7-9649-c6c919db24a4",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "foo_task",
            "to": "bar_task"
        },
        "797e0cbb-9ed5-42e1-b1e1-7911373e90f1": {
            "id": "797e0cbb-9ed5-42e1-b1e1-7911373e90f1",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "bar_task",
            "to": "baz_task"
        },
        "9e6cd9c5-8c48-4035-b754-abf341d13d3e": {
            "id": "9e6cd9c5-8c48-4035-b754-abf341d13d3e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "foo_task"
        }
    },
    "outTransitions": {
        "foo_task": [
            "c6f60049-e129-43d7-9649-c6c919db24a4"
        ],
        "bar_task": [
            "797e0cbb-9ed5-42e1-b1e1-7911373e90f1"
        ]
    },
    "inTransitions": {
        "bar_task": [
            "c6f60049-e129-43d7-9649-c6c919db24a4"
        ],
        "baz_task": [
            "797e0cbb-9ed5-42e1-b1e1-7911373e90f1"
        ],
        "foo_task": [
            "9e6cd9c5-8c48-4035-b754-abf341d13d3e"
        ]
    },
    "tokens": {
        "39c0d36b-491b-4e9a-a311-91df55d1240c": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "39c0d36b-491b-4e9a-a311-91df55d1240c",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "2a541c1b-69fb-43f9-b815-2f7dd854a00a",
                    "transitionId": "9e6cd9c5-8c48-4035-b754-abf341d13d3e",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479140658513
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "9d5f2fb5-9ebc-430a-b848-31a0fc7380ba",
                    "transitionId": "9e6cd9c5-8c48-4035-b754-abf341d13d3e",
                    "weight": 1,
                    "state": "passed",
                    "time": 15479140658517
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "f43fc8af-057c-4176-8ac2-fdbb39c559e5",
                    "transitionId": "c6f60049-e129-43d7-9649-c6c919db24a4",
                    "weight": 1,
                    "state": "opened",
                    "time": 15479140658520,
                    "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}"
                }
            ]
        }
    }
}