Issue #3189301 by Spokje, xjm, smustgrave: Use \Psr\Http\Client\ClientExceptionInterface instead of \GuzzleHttp\Exception\TransferException

merge-requests/5143/merge
Alex Pott 2024-01-30 23:47:41 +00:00
parent e94e1d770d
commit 8c89656bdd
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
14 changed files with 31 additions and 30 deletions

View File

@ -35,12 +35,12 @@ use Drupal\Core\Url;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Core\Routing\RouteObjectInterface;
use Psr\Http\Client\ClientExceptionInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Route;
use Drupal\user\Entity\User;
use GuzzleHttp\Exception\TransferException;
/**
* Do not run the task during the current installation request.
@ -1446,9 +1446,10 @@ function install_retrieve_file($uri, $destination) {
return FALSE;
}
}
catch (TransferException $e) {
catch (ClientExceptionInterface) {
return FALSE;
}
return file_put_contents($path, $data) !== FALSE;
}
@ -1466,7 +1467,7 @@ function install_check_localization_server($uri) {
\Drupal::httpClient()->head($uri);
return TRUE;
}
catch (TransferException $e) {
catch (ClientExceptionInterface) {
return FALSE;
}
}

View File

@ -11,7 +11,7 @@ use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Url;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
@ -354,7 +354,7 @@ function locale_translation_download_source($source_file, $directory = 'temporar
return $file;
}
}
catch (TransferException $exception) {
catch (ClientExceptionInterface $exception) {
\Drupal::messenger()->addError(t('Failed to fetch file due to error "%error"', ['%error' => $exception->getMessage()]));
}
catch (FileException | InvalidStreamWrapperException $e) {

View File

@ -8,7 +8,7 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Client\ClientExceptionInterface;
/**
* Retrieves and caches information about oEmbed providers.
@ -100,7 +100,7 @@ class ProviderRepository implements ProviderRepositoryInterface {
try {
$response = $this->httpClient->request('GET', $this->providersUrl);
}
catch (TransferException $e) {
catch (ClientExceptionInterface $e) {
if (isset($stored['data'])) {
// Use the stale data to fall back gracefully, but warn site
// administrators that we used stale data.

View File

@ -5,8 +5,8 @@ namespace Drupal\media\OEmbed;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Cache\CacheBackendInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\RequestOptions;
use Psr\Http\Client\ClientExceptionInterface;
// cspell:ignore nocdata
@ -68,7 +68,7 @@ class ResourceFetcher implements ResourceFetcherInterface {
RequestOptions::TIMEOUT => 5,
]);
}
catch (TransferException $e) {
catch (ClientExceptionInterface $e) {
throw new ResourceException('Could not retrieve the oEmbed resource.', $url, [], $e);
}

View File

@ -7,7 +7,7 @@ use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Client\ClientExceptionInterface;
// cspell:ignore omitscript
@ -96,7 +96,7 @@ class UrlResolver implements UrlResolverInterface {
try {
$response = $this->httpClient->get($url);
}
catch (TransferException $e) {
catch (ClientExceptionInterface) {
return FALSE;
}

View File

@ -25,7 +25,7 @@ use Drupal\media\MediaTypeInterface;
use Drupal\media\OEmbed\ResourceFetcherInterface;
use Drupal\media\OEmbed\UrlResolverInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@ -441,7 +441,7 @@ class OEmbed extends MediaSourceBase implements OEmbedInterface {
return $local_thumbnail_uri;
}
}
catch (TransferException $e) {
catch (ClientExceptionInterface $e) {
$this->logger->warning('Failed to download remote thumbnail file due to "%error".', [
'%error' => $e->getMessage(),
]);

View File

@ -14,7 +14,7 @@ use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\Plugin\Exception\BadPluginDefinitionException;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Client\ClientExceptionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
@ -360,7 +360,7 @@ class CredentialForm extends MigrateUpgradeFormBase {
try {
$this->httpClient->head($source);
}
catch (TransferException $e) {
catch (ClientExceptionInterface $e) {
$msg .= ' ' . $this->t('The server reports the following message: %error.', ['%error' => $e->getMessage()]);
$this->errors[$element['#name']] = $msg;
}

View File

@ -13,8 +13,8 @@ use Drupal\Core\Utility\Error;
use Drupal\Core\Utility\ProjectInfo;
use Drupal\Core\Extension\ExtensionVersion;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\RequestOptions;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Log\LoggerInterface;
/**
@ -115,7 +115,7 @@ final class SecurityAdvisoriesFetcher {
* retrieving the JSON feed, or if there was no stored response and
* $allow_outgoing_request was set to FALSE.
*
* @throws \GuzzleHttp\Exception\TransferException
* @throws \Psr\Http\Client\ClientExceptionInterface
* Thrown if an error occurs while retrieving security advisories.
*/
public function getSecurityAdvisories(bool $allow_outgoing_request = TRUE, int $timeout = 0): ?array {
@ -321,7 +321,7 @@ final class SecurityAdvisoriesFetcher {
try {
$response = $this->httpClient->get('https://updates.drupal.org/psa.json', $options);
}
catch (TransferException $exception) {
catch (ClientExceptionInterface $exception) {
Error::logException($this->logger, $exception);
$response = $this->httpClient->get('http://updates.drupal.org/psa.json', $options);
}

View File

@ -28,7 +28,7 @@ use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\Core\Utility\Error;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Client\ClientExceptionInterface;
use Symfony\Component\HttpFoundation\Request;
// cspell:ignore quickedit
@ -1706,7 +1706,7 @@ function _system_advisories_requirements(array &$requirements): void {
try {
$advisories = $fetcher->getSecurityAdvisories(TRUE, 5);
}
catch (TransferException $exception) {
catch (ClientExceptionInterface $exception) {
$requirements['system_advisories']['title'] = t('Critical security announcements');
$requirements['system_advisories']['severity'] = REQUIREMENT_WARNING;
$requirements['system_advisories']['description'] = ['#theme' => 'system_security_advisories_fetch_error_message'];

View File

@ -35,7 +35,7 @@ use Drupal\Core\Site\Settings;
use Drupal\Core\StreamWrapper\LocalStream;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\Url;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Client\ClientExceptionInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
@ -1145,7 +1145,7 @@ function system_retrieve_file($url, $destination = NULL, $managed = FALSE, $repl
$local = $file_system->saveData($data, $path, $replace);
}
}
catch (TransferException $exception) {
catch (ClientExceptionInterface $exception) {
\Drupal::messenger()->addError(t('Failed to fetch file due to error "%error"', ['%error' => $exception->getMessage()]));
return FALSE;
}

View File

@ -9,11 +9,11 @@ use Drupal\Core\Logger\RfcLogLevel;
use Drupal\KernelTests\KernelTestBase;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Log\LoggerInterface;
/**
@ -674,7 +674,7 @@ class SecurityAdvisoriesFetcherTest extends KernelTestBase implements LoggerInte
try {
$this->getAdvisories();
}
catch (TransferException $exception) {
catch (ClientExceptionInterface $exception) {
$this->assertSame("Server error: `GET https://updates.drupal.org/psa.json` resulted in a `500 Internal Server Error` response:\nHTTPS failed\n", $exception->getMessage());
$exception_thrown = TRUE;
}

View File

@ -7,7 +7,7 @@ use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Site\Settings;
use Drupal\Core\Utility\Error;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Log\LoggerInterface;
/**
@ -104,7 +104,7 @@ class UpdateFetcher implements UpdateFetcherInterface {
->get($url, ['headers' => ['Accept' => 'text/xml']])
->getBody();
}
catch (TransferException $exception) {
catch (ClientExceptionInterface $exception) {
Error::logException($this->logger, $exception);
if ($with_http_fallback && !str_contains($url, "http://")) {
$url = str_replace('https://', 'http://', $url);

View File

@ -40,7 +40,7 @@ use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\Exception\InvalidStreamWrapperException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Url;
use GuzzleHttp\Exception\TransferException;
use Psr\Http\Client\ClientExceptionInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
@ -233,7 +233,7 @@ function update_manager_file_get($url) {
$data = (string) \Drupal::httpClient()->get($url)->getBody();
return \Drupal::service('file_system')->saveData($data, $local, FileSystemInterface::EXISTS_REPLACE);
}
catch (TransferException $exception) {
catch (ClientExceptionInterface $exception) {
\Drupal::messenger()->addError(t('Failed to fetch file due to error "%error"', ['%error' => $exception->getMessage()]));
}
catch (FileException | InvalidStreamWrapperException $e) {

View File

@ -138,8 +138,8 @@ class DrupalTestBrowser extends AbstractBrowser {
try {
$response = $this->getClient()->request($method, $uri, $request_options);
}
// Catch RequestException rather than TransferException because we want
// to re-throw the exception whenever the response is NULL, and
// Catch RequestException rather than ClientExceptionInterface because we
// want to re-throw the exception whenever the response is NULL, and
// ConnectException always has a NULL response.
catch (RequestException $e) {
if (!$e->hasResponse()) {