Skip to content

Commit 0be8438

Browse files
[11.x] Include ConnectionException in ConnectionFailed events (#52069)
* Update PendingRequest.php * Update ConnectionFailed.php * Update ConnectionFailed.php * Update ConnectionFailed.php * Update ConnectionFailed.php * Update ConnectionFailed.php * Update ConnectionFailed.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 1dd5027 commit 0be8438

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/Illuminate/Http/Client/Events/ConnectionFailed.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Http\Client\Events;
44

55
use Illuminate\Http\Client\Request;
6+
use Illuminate\Http\Client\ConnectionException;
67

78
class ConnectionFailed
89
{
@@ -13,14 +14,23 @@ class ConnectionFailed
1314
*/
1415
public $request;
1516

17+
/**
18+
* The exception instance.
19+
*
20+
* @var \Illuminate\Http\Client\ConnectionException
21+
*/
22+
public $exception;
23+
1624
/**
1725
* Create a new event instance.
1826
*
1927
* @param \Illuminate\Http\Client\Request $request
28+
* @param \Illuminate\Http\Client\ConnectionException $exception
2029
* @return void
2130
*/
22-
public function __construct(Request $request)
31+
public function __construct(Request $request, ConnectionException $exception)
2332
{
2433
$this->request = $request;
34+
$this->exception = $exception;
2535
}
2636
}

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -938,9 +938,11 @@ public function send(string $method, string $url, array $options = [])
938938
}
939939
});
940940
} catch (ConnectException $e) {
941-
$this->dispatchConnectionFailedEvent(new Request($e->getRequest()));
941+
$exception = new ConnectionException($e->getMessage(), 0, $e);
942942

943-
throw new ConnectionException($e->getMessage(), 0, $e);
943+
$this->dispatchConnectionFailedEvent(new Request($e->getRequest()), $exception);
944+
945+
throw $exception;
944946
}
945947
}, $this->retryDelay ?? 100, function ($exception) use (&$shouldRetry) {
946948
$result = $shouldRetry ?? ($this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $exception, $this) : true);
@@ -1028,9 +1030,11 @@ protected function makePromise(string $method, string $url, array $options = [],
10281030
})
10291031
->otherwise(function (OutOfBoundsException|TransferException $e) {
10301032
if ($e instanceof ConnectException) {
1031-
$this->dispatchConnectionFailedEvent(new Request($e->getRequest()));
1033+
$exception = new ConnectionException($e->getMessage(), 0, $e);
1034+
1035+
$this->dispatchConnectionFailedEvent(new Request($e->getRequest()), $exception);
10321036

1033-
return new ConnectionException($e->getMessage(), 0, $e);
1037+
return $exception;
10341038
}
10351039

10361040
return $e instanceof RequestException && $e->hasResponse() ? $this->populateResponse($this->newResponse($e->getResponse())) : $e;
@@ -1496,12 +1500,13 @@ protected function dispatchResponseReceivedEvent(Response $response)
14961500
* Dispatch the ConnectionFailed event if a dispatcher is available.
14971501
*
14981502
* @param \Illuminate\Http\Client\Request $request
1503+
* @param \Illuminate\Http\Client\ConnectionException $exception
14991504
* @return void
15001505
*/
1501-
protected function dispatchConnectionFailedEvent(Request $request)
1506+
protected function dispatchConnectionFailedEvent(Request $request, ConnectionException $exception)
15021507
{
15031508
if ($dispatcher = $this->factory?->getDispatcher()) {
1504-
$dispatcher->dispatch(new ConnectionFailed($request));
1509+
$dispatcher->dispatch(new ConnectionFailed($request, $exception));
15051510
}
15061511
}
15071512

0 commit comments

Comments
 (0)