Skip to content

Commit c31126b

Browse files
pandiselvammPandi Selvamtaylorotwell
authored
[11.x] Fix: Custom Exceptions with Multiple Arguments does not properly rein… (#54705)
* Fix: Custom Exceptions with Multiple Arguments does not properly reinstantiate with Concurrency::run() 54500 * Import Class added * StyleCI issue fixed * Integration Test Case Added * StyleCI issue fixed * StyleCI issue fixed * Test Cases function renamed * Process Driver Updated for Default Exception * Style CI Issue fixed * Remove Unused class * formatting --------- Co-authored-by: Pandi Selvam <pandi.selvam@terrificminds.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 120f07f commit c31126b

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/Illuminate/Concurrency/Console/InvokeSerializedClosureCommand.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Concurrency\Console;
44

55
use Illuminate\Console\Command;
6+
use ReflectionClass;
67
use Symfony\Component\Console\Attribute\AsCommand;
78
use Throwable;
89

@@ -51,12 +52,27 @@ public function handle()
5152
} catch (Throwable $e) {
5253
report($e);
5354

55+
$reflection = new ReflectionClass($e);
56+
$constructor = $reflection->getConstructor();
57+
$parameters = [];
58+
59+
if ($constructor) {
60+
$declaringClass = $constructor->getDeclaringClass()->getName();
61+
62+
if ($declaringClass === $reflection->getName()) {
63+
foreach ($constructor->getParameters() as $parameter) {
64+
$parameters[$parameter->name] = $e->{$parameter->name} ?? null;
65+
}
66+
}
67+
}
68+
5469
$this->output->write(json_encode([
5570
'successful' => false,
5671
'exception' => get_class($e),
5772
'message' => $e->getMessage(),
5873
'file' => $e->getFile(),
5974
'line' => $e->getLine(),
75+
'parameters' => $parameters,
6076
]));
6177
}
6278
}

src/Illuminate/Concurrency/ProcessDriver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public function run(Closure|array $tasks): array
4848

4949
if (! $result['successful']) {
5050
throw new $result['exception'](
51-
$result['message']
51+
...(! empty(array_filter($result['parameters']))
52+
? $result['parameters']
53+
: [$result['message']])
5254
);
5355
}
5456

tests/Integration/Concurrency/ConcurrencyTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Illuminate\Tests\Integration\Concurrency;
44

5+
use Exception;
56
use Illuminate\Concurrency\ProcessDriver;
67
use Illuminate\Foundation\Application;
78
use Illuminate\Process\Factory as ProcessFactory;
9+
use Illuminate\Support\Facades\Concurrency;
810
use Orchestra\Testbench\TestCase;
911
use PHPUnit\Framework\Attributes\RequiresOperatingSystem;
1012

@@ -49,4 +51,55 @@ public function testRunHandlerProcessErrorCode()
4951
fn () => exit(1),
5052
]);
5153
}
54+
55+
public function testRunHandlerProcessErrorWithDefaultExceptionWithoutParam()
56+
{
57+
$this->expectException(\Exception::class);
58+
$this->expectExceptionMessage('This is a different exception');
59+
60+
Concurrency::run([
61+
fn () => throw new Exception(
62+
'This is a different exception',
63+
),
64+
]);
65+
}
66+
67+
public function testRunHandlerProcessErrorWithCustomExceptionWithoutParam()
68+
{
69+
$this->expectException(ExceptionWithoutParam::class);
70+
$this->expectExceptionMessage('Test');
71+
Concurrency::run([
72+
fn () => throw new ExceptionWithoutParam('Test'),
73+
]);
74+
}
75+
76+
public function testRunHandlerProcessErrorWithCustomExceptionWithParam()
77+
{
78+
$this->expectException(ExceptionWithParam::class);
79+
$this->expectExceptionMessage('API request to https://api.example.com failed with status 400 Bad Request');
80+
Concurrency::run([
81+
fn () => throw new ExceptionWithParam(
82+
'https://api.example.com',
83+
400,
84+
'Bad Request',
85+
'Invalid payload'
86+
),
87+
]);
88+
}
89+
}
90+
91+
class ExceptionWithoutParam extends Exception
92+
{
93+
}
94+
95+
class ExceptionWithParam extends Exception
96+
{
97+
public function __construct(
98+
public string $uri,
99+
public int $statusCode,
100+
public string $reason,
101+
public string|array $responseBody = '',
102+
) {
103+
parent::__construct("API request to {$uri} failed with status $statusCode $reason");
104+
}
52105
}

0 commit comments

Comments
 (0)