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": "198f5ad2-729d-4688-aa5b-300854c48733",
    "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": {
        "976672b2-6ad6-4c7c-9731-3be2a3e5973e": {
            "id": "976672b2-6ad6-4c7c-9731-3be2a3e5973e",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "foo_task",
            "to": "bar_task"
        },
        "7dab2116-41d0-4057-88f9-fd29c4a7ceb5": {
            "id": "7dab2116-41d0-4057-88f9-fd29c4a7ceb5",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "from": "bar_task",
            "to": "baz_task"
        },
        "71f54ee0-357b-4758-8183-7ee8ed843858": {
            "id": "71f54ee0-357b-4758-8183-7ee8ed843858",
            "weight": 1,
            "async": false,
            "active": true,
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Transition.json",
            "to": "foo_task"
        }
    },
    "outTransitions": {
        "foo_task": [
            "976672b2-6ad6-4c7c-9731-3be2a3e5973e"
        ],
        "bar_task": [
            "7dab2116-41d0-4057-88f9-fd29c4a7ceb5"
        ]
    },
    "inTransitions": {
        "bar_task": [
            "976672b2-6ad6-4c7c-9731-3be2a3e5973e"
        ],
        "baz_task": [
            "7dab2116-41d0-4057-88f9-fd29c4a7ceb5"
        ],
        "foo_task": [
            "71f54ee0-357b-4758-8183-7ee8ed843858"
        ]
    },
    "tokens": {
        "ac3e2d13-445d-48dd-9ef3-f25930250eb9": {
            "schema": "http:\/\/pvm.forma-pro.com\/schemas\/Token.json",
            "id": "ac3e2d13-445d-48dd-9ef3-f25930250eb9",
            "transitions": [
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "ebc999dc-3851-4dfe-a6b3-637e350cc32d",
                    "transitionId": "71f54ee0-357b-4758-8183-7ee8ed843858",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968743313101
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "dda56f39-e137-4ee9-a0d0-221a5f391a12",
                    "transitionId": "71f54ee0-357b-4758-8183-7ee8ed843858",
                    "weight": 1,
                    "state": "passed",
                    "time": 15968743313113
                },
                {
                    "schema": "http:\/\/pvm.forma-pro.com\/schemas\/TokenTransition.json",
                    "id": "1025cde6-347d-4a30-9a6b-74029a6b59dd",
                    "transitionId": "976672b2-6ad6-4c7c-9731-3be2a3e5973e",
                    "weight": 1,
                    "state": "opened",
                    "time": 15968743313115,
                    "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}"
                }
            ]
        }
    }
}