Skip to content

Commit 8487ad1

Browse files
authored
Non-standard exceptions (#339)
1 parent d94bf70 commit 8487ad1

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/ActivityStub.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use React\Promise\Deferred;
1010
use React\Promise\PromiseInterface;
1111
use function React\Promise\resolve;
12+
use RuntimeException;
1213
use Throwable;
1314
use Workflow\Serializers\Serializer;
1415

@@ -58,7 +59,17 @@ public static function make($activity, ...$arguments): PromiseInterface
5859
array_key_exists('class', $result) &&
5960
is_subclass_of($result['class'], Throwable::class)
6061
) {
61-
throw new $result['class']($result['message'], (int) $result['code']);
62+
try {
63+
$throwable = new $result['class']($result['message'] ?? '', (int) ($result['code'] ?? 0));
64+
} catch (Throwable $throwable) {
65+
throw new RuntimeException(
66+
sprintf('[%s] %s', $result['class'], (string) ($result['message'] ?? '')),
67+
(int) ($result['code'] ?? 0),
68+
$throwable
69+
);
70+
}
71+
72+
throw $throwable;
6273
}
6374
return resolve($result);
6475
}

tests/Unit/ActivityStubTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tests\Unit;
66

77
use Exception;
8+
use RuntimeException;
89
use Tests\Fixtures\TestActivity;
910
use Tests\Fixtures\TestWorkflow;
1011
use Tests\TestCase;
@@ -92,6 +93,36 @@ public function testLoadsStoredException(): void
9293
$this->assertSame('test', $result['message']);
9394
}
9495

96+
public function testLoadsStoredExceptionWithNonStandardConstructor(): void
97+
{
98+
$this->expectException(RuntimeException::class);
99+
$this->expectExceptionMessage(
100+
'[' . ActivityStubNonStandardConstructorException::class . '] test from context (1)'
101+
);
102+
$this->expectExceptionCode(123);
103+
104+
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
105+
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
106+
$storedWorkflow->update([
107+
'arguments' => Serializer::serialize([]),
108+
'status' => WorkflowPendingStatus::$name,
109+
]);
110+
$storedWorkflow->logs()
111+
->create([
112+
'index' => 0,
113+
'now' => WorkflowStub::now(),
114+
'class' => TestActivity::class,
115+
'result' => Serializer::serialize(
116+
new ActivityStubNonStandardConstructorException('context', 'test', ['binding'], 123)
117+
),
118+
]);
119+
120+
ActivityStub::make(TestActivity::class)
121+
->then(static function ($value) use (&$result) {
122+
$result = $value;
123+
});
124+
}
125+
95126
public function testAll(): void
96127
{
97128
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
@@ -142,3 +173,11 @@ public function testAsync(): void
142173
$this->assertSame('test', $result);
143174
}
144175
}
176+
177+
final class ActivityStubNonStandardConstructorException extends Exception
178+
{
179+
public function __construct(string $connection, string $message, array $bindings, int $code)
180+
{
181+
parent::__construct($message . ' from ' . $connection . ' (' . count($bindings) . ')', $code);
182+
}
183+
}

0 commit comments

Comments
 (0)