From 8c89656bddd68545b5251b82fc48ed6e7eabb8ba Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 30 Jan 2024 23:47:41 +0000 Subject: [PATCH] Issue #3189301 by Spokje, xjm, smustgrave: Use \Psr\Http\Client\ClientExceptionInterface instead of \GuzzleHttp\Exception\TransferException --- core/includes/install.core.inc | 7 ++++--- core/modules/locale/locale.batch.inc | 4 ++-- core/modules/media/src/OEmbed/ProviderRepository.php | 4 ++-- core/modules/media/src/OEmbed/ResourceFetcher.php | 4 ++-- core/modules/media/src/OEmbed/UrlResolver.php | 4 ++-- core/modules/media/src/Plugin/media/Source/OEmbed.php | 4 ++-- core/modules/migrate_drupal_ui/src/Form/CredentialForm.php | 4 ++-- .../src/SecurityAdvisories/SecurityAdvisoriesFetcher.php | 6 +++--- core/modules/system/system.install | 4 ++-- core/modules/system/system.module | 4 ++-- .../SecurityAdvisories/SecurityAdvisoriesFetcherTest.php | 4 ++-- core/modules/update/src/UpdateFetcher.php | 4 ++-- core/modules/update/update.manager.inc | 4 ++-- core/tests/Drupal/Tests/DrupalTestBrowser.php | 4 ++-- 14 files changed, 31 insertions(+), 30 deletions(-) diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 81c2151d2a0..a34fdc8c9fd 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -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; } } diff --git a/core/modules/locale/locale.batch.inc b/core/modules/locale/locale.batch.inc index 9cdadb31544..e163cd9c618 100644 --- a/core/modules/locale/locale.batch.inc +++ b/core/modules/locale/locale.batch.inc @@ -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) { diff --git a/core/modules/media/src/OEmbed/ProviderRepository.php b/core/modules/media/src/OEmbed/ProviderRepository.php index b62d0ccac21..141c59720a1 100644 --- a/core/modules/media/src/OEmbed/ProviderRepository.php +++ b/core/modules/media/src/OEmbed/ProviderRepository.php @@ -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. diff --git a/core/modules/media/src/OEmbed/ResourceFetcher.php b/core/modules/media/src/OEmbed/ResourceFetcher.php index 789fb82aeb5..76c2d8b3b7f 100644 --- a/core/modules/media/src/OEmbed/ResourceFetcher.php +++ b/core/modules/media/src/OEmbed/ResourceFetcher.php @@ -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); } diff --git a/core/modules/media/src/OEmbed/UrlResolver.php b/core/modules/media/src/OEmbed/UrlResolver.php index 716745ab762..0c0ae933777 100644 --- a/core/modules/media/src/OEmbed/UrlResolver.php +++ b/core/modules/media/src/OEmbed/UrlResolver.php @@ -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; } diff --git a/core/modules/media/src/Plugin/media/Source/OEmbed.php b/core/modules/media/src/Plugin/media/Source/OEmbed.php index 14f677acf09..23c08b9fdd0 100644 --- a/core/modules/media/src/Plugin/media/Source/OEmbed.php +++ b/core/modules/media/src/Plugin/media/Source/OEmbed.php @@ -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(), ]); diff --git a/core/modules/migrate_drupal_ui/src/Form/CredentialForm.php b/core/modules/migrate_drupal_ui/src/Form/CredentialForm.php index 5e70200feab..888e68c22f9 100644 --- a/core/modules/migrate_drupal_ui/src/Form/CredentialForm.php +++ b/core/modules/migrate_drupal_ui/src/Form/CredentialForm.php @@ -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; } diff --git a/core/modules/system/src/SecurityAdvisories/SecurityAdvisoriesFetcher.php b/core/modules/system/src/SecurityAdvisories/SecurityAdvisoriesFetcher.php index 3b29384231a..cf1c4353230 100644 --- a/core/modules/system/src/SecurityAdvisories/SecurityAdvisoriesFetcher.php +++ b/core/modules/system/src/SecurityAdvisories/SecurityAdvisoriesFetcher.php @@ -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); } diff --git a/core/modules/system/system.install b/core/modules/system/system.install index a7d7478614b..2029f6608d9 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -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']; diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 1d7b4e656f2..2de12831986 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -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; } diff --git a/core/modules/system/tests/src/Kernel/SecurityAdvisories/SecurityAdvisoriesFetcherTest.php b/core/modules/system/tests/src/Kernel/SecurityAdvisories/SecurityAdvisoriesFetcherTest.php index 0bcb31667d5..84172eb4b74 100644 --- a/core/modules/system/tests/src/Kernel/SecurityAdvisories/SecurityAdvisoriesFetcherTest.php +++ b/core/modules/system/tests/src/Kernel/SecurityAdvisories/SecurityAdvisoriesFetcherTest.php @@ -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; } diff --git a/core/modules/update/src/UpdateFetcher.php b/core/modules/update/src/UpdateFetcher.php index 9a6704ac68f..6a2c7372b6e 100644 --- a/core/modules/update/src/UpdateFetcher.php +++ b/core/modules/update/src/UpdateFetcher.php @@ -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); diff --git a/core/modules/update/update.manager.inc b/core/modules/update/update.manager.inc index a0c2259661d..cd9071040aa 100644 --- a/core/modules/update/update.manager.inc +++ b/core/modules/update/update.manager.inc @@ -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) { diff --git a/core/tests/Drupal/Tests/DrupalTestBrowser.php b/core/tests/Drupal/Tests/DrupalTestBrowser.php index ddcc3dd908f..6c9b10b3ff1 100644 --- a/core/tests/Drupal/Tests/DrupalTestBrowser.php +++ b/core/tests/Drupal/Tests/DrupalTestBrowser.php @@ -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()) {