Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 692d0e7

Browse filesBrowse files
committed
feature #34457 Added context to exceptions thrown in apply method (koenreiniers)
This PR was squashed before being merged into the 5.1-dev branch (closes #34457). Discussion ---------- Added context to exceptions thrown in apply method | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | | License | MIT | Doc PR | During the workflow and state machines workshop at SymfonyCon, we noticed that the context in the apply method was not passed to the exceptions that are thrown. This could prove to be convenient for debugging purposes. Commits ------- 8f86c33 Added context to exceptions thrown in apply method
2 parents dab6732 + 8f86c33 commit 692d0e7
Copy full SHA for 692d0e7

File tree

6 files changed

+36
-11
lines changed
Filter options

6 files changed

+36
-11
lines changed

‎src/Symfony/Component/Workflow/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* Added context to `TransitionException` and its child classes whenever they are thrown in `Workflow::apply()`
8+
49
5.0.0
510
-----
611

‎src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/Exception/NotEnabledTransitionException.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ class NotEnabledTransitionException extends TransitionException
2323
{
2424
private $transitionBlockerList;
2525

26-
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, TransitionBlockerList $transitionBlockerList)
26+
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, TransitionBlockerList $transitionBlockerList, array $context = [])
2727
{
28-
parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not enabled for workflow "%s".', $transitionName, $workflow->getName()));
28+
parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not enabled for workflow "%s".', $transitionName, $workflow->getName()), $context);
2929

3030
$this->transitionBlockerList = $transitionBlockerList;
3131
}

‎src/Symfony/Component/Workflow/Exception/TransitionException.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/Exception/TransitionException.php
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ class TransitionException extends LogicException
2222
private $subject;
2323
private $transitionName;
2424
private $workflow;
25+
private $context;
2526

26-
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, string $message)
27+
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, string $message, array $context = [])
2728
{
2829
parent::__construct($message);
2930

3031
$this->subject = $subject;
3132
$this->transitionName = $transitionName;
3233
$this->workflow = $workflow;
34+
$this->context = $context;
3335
}
3436

3537
public function getSubject()
@@ -46,4 +48,9 @@ public function getWorkflow(): WorkflowInterface
4648
{
4749
return $this->workflow;
4850
}
51+
52+
public function getContext(): array
53+
{
54+
return $this->context;
55+
}
4956
}

‎src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/Exception/UndefinedTransitionException.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
*/
2121
class UndefinedTransitionException extends TransitionException
2222
{
23-
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow)
23+
public function __construct(object $subject, string $transitionName, WorkflowInterface $workflow, array $context = [])
2424
{
25-
parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not defined for workflow "%s".', $transitionName, $workflow->getName()));
25+
parent::__construct($subject, $transitionName, $workflow, sprintf('Transition "%s" is not defined for workflow "%s".', $transitionName, $workflow->getName()), $context);
2626
}
2727
}

‎src/Symfony/Component/Workflow/Tests/WorkflowTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/Tests/WorkflowTest.php
+17-4Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\Workflow\Event\GuardEvent;
1010
use Symfony\Component\Workflow\Event\TransitionEvent;
1111
use Symfony\Component\Workflow\Exception\NotEnabledTransitionException;
12+
use Symfony\Component\Workflow\Exception\UndefinedTransitionException;
1213
use Symfony\Component\Workflow\Marking;
1314
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
1415
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
@@ -249,23 +250,34 @@ public function testBuildTransitionBlockerListReturnsReasonsProvidedInGuards()
249250

250251
public function testApplyWithNotExisingTransition()
251252
{
252-
$this->expectException('Symfony\Component\Workflow\Exception\UndefinedTransitionException');
253-
$this->expectExceptionMessage('Transition "404 Not Found" is not defined for workflow "unnamed".');
254253
$definition = $this->createComplexWorkflowDefinition();
255254
$subject = new Subject();
256255
$workflow = new Workflow($definition, new MethodMarkingStore());
256+
$context = [
257+
'lorem' => 'ipsum',
258+
];
259+
260+
try {
261+
$workflow->apply($subject, '404 Not Found', $context);
257262

258-
$workflow->apply($subject, '404 Not Found');
263+
$this->fail('Should throw an exception');
264+
} catch (UndefinedTransitionException $e) {
265+
$this->assertSame('Transition "404 Not Found" is not defined for workflow "unnamed".', $e->getMessage());
266+
$this->assertSame($e->getContext(), $context);
267+
}
259268
}
260269

261270
public function testApplyWithNotEnabledTransition()
262271
{
263272
$definition = $this->createComplexWorkflowDefinition();
264273
$subject = new Subject();
265274
$workflow = new Workflow($definition, new MethodMarkingStore());
275+
$context = [
276+
'lorem' => 'ipsum',
277+
];
266278

267279
try {
268-
$workflow->apply($subject, 't2');
280+
$workflow->apply($subject, 't2', $context);
269281

270282
$this->fail('Should throw an exception');
271283
} catch (NotEnabledTransitionException $e) {
@@ -276,6 +288,7 @@ public function testApplyWithNotEnabledTransition()
276288
$this->assertSame($e->getWorkflow(), $workflow);
277289
$this->assertSame($e->getSubject(), $subject);
278290
$this->assertSame($e->getTransitionName(), 't2');
291+
$this->assertSame($e->getContext(), $context);
279292
}
280293
}
281294

‎src/Symfony/Component/Workflow/Workflow.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/Workflow.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ public function apply(object $subject, string $transitionName, array $context =
189189
}
190190

191191
if (!$transitionBlockerList) {
192-
throw new UndefinedTransitionException($subject, $transitionName, $this);
192+
throw new UndefinedTransitionException($subject, $transitionName, $this, $context);
193193
}
194194

195195
if (!$applied) {
196-
throw new NotEnabledTransitionException($subject, $transitionName, $this, $transitionBlockerList);
196+
throw new NotEnabledTransitionException($subject, $transitionName, $this, $transitionBlockerList, $context);
197197
}
198198

199199
return $marking;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.