Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Include ConnectionException in ConnectionFailed events #52069

Merged
merged 7 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -934,9 +934,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 @@ -1024,9 +1026,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 @@ -1492,12 +1496,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