Skip to content

Commit 29f585a

Browse files
authored
Merge pull request #143 from laravel/fix-exception-type-declaration
Ensure timout exception always recieves an array.
2 parents c5d53bc + 9c73452 commit 29f585a

File tree

3 files changed

+99
-4
lines changed

3 files changed

+99
-4
lines changed

src/Exceptions/TimeoutException.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ class TimeoutException extends Exception
99
/**
1010
* The output returned from the operation.
1111
*
12-
* @var array|null
12+
* @var array
1313
*/
1414
public $output;
1515

1616
/**
1717
* Create a new exception instance.
1818
*
19-
* @param array|null $output
19+
* @param array $output
2020
* @return void
2121
*/
22-
public function __construct(array $output = null)
22+
public function __construct(array $output)
2323
{
2424
parent::__construct('Script timed out while waiting for the process to complete.');
2525

@@ -29,7 +29,7 @@ public function __construct(array $output = null)
2929
/**
3030
* The output returned from the operation.
3131
*
32-
* @return array|null
32+
* @return array
3333
*/
3434
public function output()
3535
{

src/MakesHttpRequests.php

+8
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ public function retry($timeout, $callback, $sleep = 5)
141141
goto beginning;
142142
}
143143

144+
if ($output === null || $output === false) {
145+
$output = [];
146+
}
147+
148+
if (! is_array($output)) {
149+
$output = [$output];
150+
}
151+
144152
throw new TimeoutException($output);
145153
}
146154
}

tests/ForgeSDKTest.php

+87
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
use GuzzleHttp\Psr7\Response;
77
use Laravel\Forge\Exceptions\FailedActionException;
88
use Laravel\Forge\Exceptions\NotFoundException;
9+
use Laravel\Forge\Exceptions\TimeoutException;
910
use Laravel\Forge\Exceptions\ValidationException;
1011
use Laravel\Forge\Forge;
12+
use Laravel\Forge\MakesHttpRequests;
1113
use Mockery;
1214
use PHPUnit\Framework\TestCase;
1315

@@ -87,4 +89,89 @@ public function test_handling_failed_action_errors()
8789
$this->assertSame('Error!', $e->getMessage());
8890
}
8991
}
92+
93+
public function testRetryHandlesFalseResultFromClosure()
94+
{
95+
$requestMaker = new class()
96+
{
97+
use MakesHttpRequests;
98+
};
99+
100+
try {
101+
$requestMaker->retry(0, function () {
102+
return false;
103+
}, 0);
104+
$this->fail();
105+
} catch (TimeoutException $e) {
106+
$this->assertSame([], $e->output());
107+
}
108+
}
109+
110+
public function testRetryHandlesNullResultFromClosure()
111+
{
112+
$requestMaker = new class()
113+
{
114+
use MakesHttpRequests;
115+
};
116+
117+
try {
118+
$requestMaker->retry(0, function () {
119+
return null;
120+
}, 0);
121+
$this->fail();
122+
} catch (TimeoutException $e) {
123+
$this->assertSame([], $e->output());
124+
}
125+
}
126+
127+
public function testRetryHandlesFalseyStringResultFromClosure()
128+
{
129+
$requestMaker = new class()
130+
{
131+
use MakesHttpRequests;
132+
};
133+
134+
try {
135+
$requestMaker->retry(0, function () {
136+
return '';
137+
}, 0);
138+
$this->fail();
139+
} catch (TimeoutException $e) {
140+
$this->assertSame([''], $e->output());
141+
}
142+
}
143+
144+
public function testRetryHandlesFalseyNumerResultFromClosure()
145+
{
146+
$requestMaker = new class()
147+
{
148+
use MakesHttpRequests;
149+
};
150+
151+
try {
152+
$requestMaker->retry(0, function () {
153+
return 0;
154+
}, 0);
155+
$this->fail();
156+
} catch (TimeoutException $e) {
157+
$this->assertSame([0], $e->output());
158+
}
159+
}
160+
161+
public function testRetryHandlesFalseyArrayResultFromClosure()
162+
{
163+
$requestMaker = new class()
164+
{
165+
use MakesHttpRequests;
166+
};
167+
168+
try {
169+
$requestMaker->retry(0, function () {
170+
return [];
171+
}, 0);
172+
$this->fail();
173+
} catch (TimeoutException $e) {
174+
$this->assertSame([], $e->output());
175+
}
176+
}
90177
}

0 commit comments

Comments
 (0)