Skip to content

Commit 9e7a410

Browse files
committed
bug #35245 [HttpClient] fix exception in case of PSR17 discovery failure (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] fix exception in case of PSR17 discovery failure | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - On symfony.com, we need to use HTTPlug for 3rd party libs. When `nyholm/psr7` is not installed, we currently see an exception saying `No HTTPlug clients found.` from `HttpClientDiscovery`. This fixes the message by correctly suggesting `nyholm/psr7` instead, since there *is* an HTTPlug client: `HttplugClient` from our HttpClient component. It's quite unfortunate that `guzzle/psr7` provides no PSR17 factory yet, because that would have solved some part of this deps mess. /cc @Nyholm @sagikazarmark FYI Note that https://packagist.org/providers/psr/http-factory-implementation lists `guzzle/psr7` but this is a wrong solution: no tagged release of it is PSR17-compatible, which means installing it doesn't solve the issue. Commits ------- 96e70a4 [HttpClient] fix exception in case of PSR17 discovery failure
2 parents ada4c56 + 96e70a4 commit 9e7a410

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/Symfony/Component/HttpClient/HttplugClient.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Http\Client\Exception\RequestException;
1717
use Http\Client\HttpAsyncClient;
1818
use Http\Client\HttpClient as HttplugInterface;
19+
use Http\Discovery\Exception\NotFoundException;
1920
use Http\Discovery\Psr17FactoryDiscovery;
2021
use Http\Message\RequestFactory;
2122
use Http\Message\StreamFactory;
@@ -75,9 +76,13 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
7576
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been provided. Try running "composer require nyholm/psr7".');
7677
}
7778

78-
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null;
79-
$this->responseFactory = $this->responseFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory();
80-
$this->streamFactory = $this->streamFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory();
79+
try {
80+
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null;
81+
$this->responseFactory = $this->responseFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory();
82+
$this->streamFactory = $this->streamFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory();
83+
} catch (NotFoundException $e) {
84+
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been found. Try running "composer require nyholm/psr7".', 0, $e);
85+
}
8186
}
8287

8388
$this->waitLoop = new HttplugWaitLoop($this->client, $this->promisePool, $this->responseFactory, $this->streamFactory);

src/Symfony/Component/HttpClient/Psr18Client.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Http\Discovery\Exception\NotFoundException;
1415
use Http\Discovery\Psr17FactoryDiscovery;
1516
use Nyholm\Psr7\Factory\Psr17Factory;
1617
use Nyholm\Psr7\Request;
@@ -68,9 +69,13 @@ public function __construct(HttpClientInterface $client = null, ResponseFactoryI
6869
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\Psr18Client" as no PSR-17 factories have been provided. Try running "composer require nyholm/psr7".');
6970
}
7071

71-
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null;
72-
$this->responseFactory = $this->responseFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory();
73-
$this->streamFactory = $this->streamFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory();
72+
try {
73+
$psr17Factory = class_exists(Psr17Factory::class, false) ? new Psr17Factory() : null;
74+
$this->responseFactory = $this->responseFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findResponseFactory();
75+
$this->streamFactory = $this->streamFactory ?? $psr17Factory ?? Psr17FactoryDiscovery::findStreamFactory();
76+
} catch (NotFoundException $e) {
77+
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\HttplugClient" as no PSR-17 factories have been found. Try running "composer require nyholm/psr7".', 0, $e);
78+
}
7479
}
7580

7681
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy