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": "87cd4f7b-0fc9-4b1c-bc4e-7b07edf8dcf8",
    "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": {
        "c957d273-62f2-48ee-9074-526d21147fd8": {
            "id": "c957d273-62f2-48ee-9074-526d21147fd8",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "foo_task",
            "to": "bar_task"
        },
        "b66550fa-8431-4879-9281-e81eb9511f0a": {
            "id": "b66550fa-8431-4879-9281-e81eb9511f0a",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "bar_task",
            "to": "baz_task"
        },
        "55c8ba56-c7e0-4964-afc5-e0bcf6579d85": {
            "id": "55c8ba56-c7e0-4964-afc5-e0bcf6579d85",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "foo_task"
        }
    },
    "outTransitions": {
        "foo_task": [
            "c957d273-62f2-48ee-9074-526d21147fd8"
        ],
        "bar_task": [
            "b66550fa-8431-4879-9281-e81eb9511f0a"
        ]
    },
    "inTransitions": {
        "bar_task": [
            "c957d273-62f2-48ee-9074-526d21147fd8"
        ],
        "baz_task": [
            "b66550fa-8431-4879-9281-e81eb9511f0a"
        ],
        "foo_task": [
            "55c8ba56-c7e0-4964-afc5-e0bcf6579d85"
        ]
    },
    "tokens": {
        "a5d62428-77a5-4601-9f39-7c197cc95862": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "a5d62428-77a5-4601-9f39-7c197cc95862",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "610ccac4-7c49-44c3-9a2c-8ecae7b8bd0d",
                    "transitionId": "55c8ba56-c7e0-4964-afc5-e0bcf6579d85",
                    "weight": 1,
                    "state": "opened",
                    "time": 15757080839402
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "8c066727-56db-403a-bc81-ed0fef7072ea",
                    "transitionId": "55c8ba56-c7e0-4964-afc5-e0bcf6579d85",
                    "weight": 1,
                    "state": "passed",
                    "time": 15757080839406
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "10ff655d-fae5-4812-b3db-54899a709bef",
                    "transitionId": "c957d273-62f2-48ee-9074-526d21147fd8",
                    "weight": 1,
                    "state": "opened",
                    "time": 15757080839409,
                    "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}"
                }
            ]
        }
    }
}