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": "51c1deb0-8b0f-489c-9512-ca1c92a1d381",
    "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": {
        "ec05bc84-7119-4162-9db8-e93d95388266": {
            "id": "ec05bc84-7119-4162-9db8-e93d95388266",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "foo_task",
            "to": "bar_task"
        },
        "01c5a3d1-4484-4313-8664-3189bb0381a3": {
            "id": "01c5a3d1-4484-4313-8664-3189bb0381a3",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "bar_task",
            "to": "baz_task"
        },
        "f7b98ce9-9777-43e5-8acc-c97dc82bd77f": {
            "id": "f7b98ce9-9777-43e5-8acc-c97dc82bd77f",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "foo_task"
        }
    },
    "outTransitions": {
        "foo_task": [
            "ec05bc84-7119-4162-9db8-e93d95388266"
        ],
        "bar_task": [
            "01c5a3d1-4484-4313-8664-3189bb0381a3"
        ]
    },
    "inTransitions": {
        "bar_task": [
            "ec05bc84-7119-4162-9db8-e93d95388266"
        ],
        "baz_task": [
            "01c5a3d1-4484-4313-8664-3189bb0381a3"
        ],
        "foo_task": [
            "f7b98ce9-9777-43e5-8acc-c97dc82bd77f"
        ]
    },
    "tokens": {
        "b280ade4-d103-414a-b188-ebab04f72e85": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "b280ade4-d103-414a-b188-ebab04f72e85",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "75572646-88b8-42d3-b877-98a5f8831758",
                    "transitionId": "f7b98ce9-9777-43e5-8acc-c97dc82bd77f",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508362508168
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ae49e5e1-b756-4dc9-9098-85f7ed6fa7ef",
                    "transitionId": "f7b98ce9-9777-43e5-8acc-c97dc82bd77f",
                    "weight": 1,
                    "state": "passed",
                    "time": 15508362508173
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "87a32a1a-cddf-4810-ab70-95ec3b4a49a3",
                    "transitionId": "ec05bc84-7119-4162-9db8-e93d95388266",
                    "weight": 1,
                    "state": "opened",
                    "time": 15508362508176,
                    "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}"
                }
            ]
        }
    }
}