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": "9b299e1a-f52f-4da9-98ee-1b84ceb32375",
    "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": {
        "e6790ea5-9bfd-4dd4-9124-424295c12a58": {
            "id": "e6790ea5-9bfd-4dd4-9124-424295c12a58",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "foo_task",
            "to": "bar_task"
        },
        "801f42bb-f735-4128-8b9e-ee7cc4efffc4": {
            "id": "801f42bb-f735-4128-8b9e-ee7cc4efffc4",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "bar_task",
            "to": "baz_task"
        },
        "fd69527c-4d5c-4557-8bdb-1df5deb09039": {
            "id": "fd69527c-4d5c-4557-8bdb-1df5deb09039",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "foo_task"
        }
    },
    "outTransitions": {
        "foo_task": [
            "e6790ea5-9bfd-4dd4-9124-424295c12a58"
        ],
        "bar_task": [
            "801f42bb-f735-4128-8b9e-ee7cc4efffc4"
        ]
    },
    "inTransitions": {
        "bar_task": [
            "e6790ea5-9bfd-4dd4-9124-424295c12a58"
        ],
        "baz_task": [
            "801f42bb-f735-4128-8b9e-ee7cc4efffc4"
        ],
        "foo_task": [
            "fd69527c-4d5c-4557-8bdb-1df5deb09039"
        ]
    },
    "tokens": {
        "14d0d081-bd2e-4b78-94a1-401ea00092cf": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "14d0d081-bd2e-4b78-94a1-401ea00092cf",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "c0fe40f9-3980-4e2e-94c9-ffae20234947",
                    "transitionId": "fd69527c-4d5c-4557-8bdb-1df5deb09039",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421461991769
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "3abe7f5a-efdf-48be-aaa7-e4e4cac3ad4b",
                    "transitionId": "fd69527c-4d5c-4557-8bdb-1df5deb09039",
                    "weight": 1,
                    "state": "passed",
                    "time": 15421461991773
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "4ed3cd65-2655-4520-9a05-520666b48560",
                    "transitionId": "e6790ea5-9bfd-4dd4-9124-424295c12a58",
                    "weight": 1,
                    "state": "opened",
                    "time": 15421461991776,
                    "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}"
                }
            ]
        }
    }
}