Skip to content

Commit

Permalink
[11.x] Include ConnectionException in ConnectionFailed events (#52069)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
alexbowers and taylorotwell authored Jul 10, 2024
1 parent 1dd5027 commit 0be8438
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/Illuminate/Http/Client/Events/ConnectionFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Http\Client\Events;

use Illuminate\Http\Client\Request;
use Illuminate\Http\Client\ConnectionException;

class ConnectionFailed
{
Expand All @@ -13,14 +14,23 @@ class ConnectionFailed
*/
public $request;

/**
* The exception instance.
*
* @var \Illuminate\Http\Client\ConnectionException
*/
public $exception;

/**
* Create a new event instance.
*
* @param \Illuminate\Http\Client\Request $request
* @param \Illuminate\Http\Client\ConnectionException $exception
* @return void
*/
public function __construct(Request $request)
public function __construct(Request $request, ConnectionException $exception)
{
$this->request = $request;
$this->exception = $exception;
}
}
17 changes: 11 additions & 6 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,9 +938,11 @@ public function send(string $method, string $url, array $options = [])
}
});
} catch (ConnectException $e) {
$this->dispatchConnectionFailedEvent(new Request($e->getRequest()));
$exception = new ConnectionException($e->getMessage(), 0, $e);

throw new ConnectionException($e->getMessage(), 0, $e);
$this->dispatchConnectionFailedEvent(new Request($e->getRequest()), $exception);

throw $exception;
}
}, $this->retryDelay ?? 100, function ($exception) use (&$shouldRetry) {
$result = $shouldRetry ?? ($this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $exception, $this) : true);
Expand Down Expand Up @@ -1028,9 +1030,11 @@ protected function makePromise(string $method, string $url, array $options = [],
})
->otherwise(function (OutOfBoundsException|TransferException $e) {
if ($e instanceof ConnectException) {
$this->dispatchConnectionFailedEvent(new Request($e->getRequest()));
$exception = new ConnectionException($e->getMessage(), 0, $e);

$this->dispatchConnectionFailedEvent(new Request($e->getRequest()), $exception);

return new ConnectionException($e->getMessage(), 0, $e);
return $exception;
}

return $e instanceof RequestException && $e->hasResponse() ? $this->populateResponse($this->newResponse($e->getResponse())) : $e;
Expand Down Expand Up @@ -1496,12 +1500,13 @@ protected function dispatchResponseReceivedEvent(Response $response)
* Dispatch the ConnectionFailed event if a dispatcher is available.
*
* @param \Illuminate\Http\Client\Request $request
* @param \Illuminate\Http\Client\ConnectionException $exception
* @return void
*/
protected function dispatchConnectionFailedEvent(Request $request)
protected function dispatchConnectionFailedEvent(Request $request, ConnectionException $exception)
{
if ($dispatcher = $this->factory?->getDispatcher()) {
$dispatcher->dispatch(new ConnectionFailed($request));
$dispatcher->dispatch(new ConnectionFailed($request, $exception));
}
}

Expand Down

0 comments on commit 0be8438

Please sign in to comment.