From 18482dedea55aa475b055b25b79fdc13fa8ad1d5 Mon Sep 17 00:00:00 2001 From: Andreas Hennings Date: Thu, 21 Nov 2024 20:43:12 +0100 Subject: [PATCH] Issue #56: Rename the exception to CollaboraNotAvailableException. --- src/Controller/ViewerController.php | 4 ++-- src/Cool/CollaboraConnection.php | 16 ++++++++-------- src/Cool/CollaboraDiscovery.php | 10 +++++----- src/Exception/CollaboraNotAvailableException.php | 16 ++++++++++++++++ src/Exception/CoolRequestException.php | 12 ------------ tests/src/ExistingSite/FetchClientUrlTest.php | 4 ++-- 6 files changed, 33 insertions(+), 29 deletions(-) create mode 100644 src/Exception/CollaboraNotAvailableException.php delete mode 100644 src/Exception/CoolRequestException.php diff --git a/src/Controller/ViewerController.php b/src/Controller/ViewerController.php index 0c0786ac..880d37a9 100644 --- a/src/Controller/ViewerController.php +++ b/src/Controller/ViewerController.php @@ -14,7 +14,7 @@ use Drupal\collabora_online\Cool\CollaboraDiscovery; use Drupal\collabora_online\Cool\CoolUtils; -use Drupal\collabora_online\Exception\CoolRequestException; +use Drupal\collabora_online\Exception\CollaboraNotAvailableException; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Render\RendererInterface; use Drupal\media\Entity\Media; @@ -61,7 +61,7 @@ public function editor(Media $media, Request $request, $edit = FALSE) { try { $wopi_client_url = $this->discovery->getWopiClientURL(); } - catch (CoolRequestException $e) { + catch (CollaboraNotAvailableException $e) { $error_msg = $this->t('The Collabora Online server is not available: @message', [ '@message' => $e->getCode() . ': ' . $e->getMessage(), ]); diff --git a/src/Cool/CollaboraConnection.php b/src/Cool/CollaboraConnection.php index d7f78bb3..9b39c31f 100644 --- a/src/Cool/CollaboraConnection.php +++ b/src/Cool/CollaboraConnection.php @@ -14,7 +14,7 @@ namespace Drupal\collabora_online\Cool; -use Drupal\collabora_online\Exception\CoolRequestException; +use Drupal\collabora_online\Exception\CollaboraNotAvailableException; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Logger\LoggerChannelInterface; use GuzzleHttp\ClientInterface; @@ -51,23 +51,23 @@ public function __construct( /** * Loads the WOPI server url from configuration. * - * @throws \Drupal\collabora_online\Exception\CoolRequestException + * @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException * The WOPI server url is misconfigured, or the protocol does not match * that of the current Drupal request. */ protected function getWopiClientServerBaseUrl(): string { $cool_settings = $this->configFactory->get('collabora_online.settings')->get('cool'); if (!$cool_settings) { - throw new CoolRequestException('The Collabora Online connection is not configured.'); + throw new CollaboraNotAvailableException('The Collabora Online connection is not configured.'); } $wopi_client_server = $cool_settings['server'] ?? NULL; if (!$wopi_client_server) { - throw new CoolRequestException('The configured Collabora Online server address is empty.'); + throw new CollaboraNotAvailableException('The configured Collabora Online server address is empty.'); } $wopi_client_server = trim($wopi_client_server); if (!preg_match('@^https?://@', $wopi_client_server)) { - throw new CoolRequestException(sprintf( + throw new CollaboraNotAvailableException(sprintf( "The configured Collabora Online server address must begin with 'http://' or 'https://'. Found '%s'.", $wopi_client_server, )); @@ -82,7 +82,7 @@ protected function getWopiClientServerBaseUrl(): string { * @return string * The full contents of discovery.xml. * - * @throws \Drupal\collabora_online\Exception\CoolRequestException + * @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException * The client url cannot be retrieved. */ public function getDiscoveryXml(): string { @@ -90,7 +90,7 @@ public function getDiscoveryXml(): string { $cool_settings = $this->configFactory->get('collabora_online.settings')->get('cool'); if (!$cool_settings) { - throw new CoolRequestException('The Collabora Online connection is not configured.'); + throw new CollaboraNotAvailableException('The Collabora Online connection is not configured.'); } $disable_checks = !empty($cool_settings['disable_cert_check']); @@ -107,7 +107,7 @@ public function getDiscoveryXml(): string { '@url' => $discovery_url, '@message' => $e->getMessage(), ]); - throw new CoolRequestException( + throw new CollaboraNotAvailableException( 'Not able to retrieve the discovery.xml file from the Collabora Online server.', previous: $e, ); diff --git a/src/Cool/CollaboraDiscovery.php b/src/Cool/CollaboraDiscovery.php index ba0b9468..44fdb424 100644 --- a/src/Cool/CollaboraDiscovery.php +++ b/src/Cool/CollaboraDiscovery.php @@ -12,7 +12,7 @@ namespace Drupal\collabora_online\Cool; -use Drupal\collabora_online\Exception\CoolRequestException; +use Drupal\collabora_online\Exception\CollaboraNotAvailableException; /** * Service to fetch a WOPI client url. @@ -39,7 +39,7 @@ public function __construct( * @return string * The WOPI client url, or NULL on failure. * - * @throws \Drupal\collabora_online\Exception\CoolRequestException + * @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException * The client url cannot be retrieved. */ public function getWopiClientURL(string $mimetype = 'text/plain'): string { @@ -49,7 +49,7 @@ public function getWopiClientURL(string $mimetype = 'text/plain'): string { if (!empty($result[0]['urlsrc'][0])) { return (string) $result[0]['urlsrc'][0]; } - throw new CoolRequestException('The requested mime type is not handled.'); + throw new CollaboraNotAvailableException('The requested mime type is not handled.'); } /** @@ -58,7 +58,7 @@ public function getWopiClientURL(string $mimetype = 'text/plain'): string { * @return \SimpleXMLElement * Parsed xml from the discovery.xml. * - * @throws \Drupal\collabora_online\Exception\CoolRequestException + * @throws \Drupal\collabora_online\Exception\CollaboraNotAvailableException * Fetching the discovery.xml failed, or the result is not valid xml. */ protected function getParsedXml(): \SimpleXMLElement { @@ -66,7 +66,7 @@ protected function getParsedXml(): \SimpleXMLElement { $discovery_parsed = simplexml_load_string($xml); if (!$discovery_parsed) { - throw new CoolRequestException('The retrieved discovery.xml file is not a valid XML file.'); + throw new CollaboraNotAvailableException('The retrieved discovery.xml file is not a valid XML file.'); } return $discovery_parsed; diff --git a/src/Exception/CollaboraNotAvailableException.php b/src/Exception/CollaboraNotAvailableException.php new file mode 100644 index 00000000..4f6ebadd --- /dev/null +++ b/src/Exception/CollaboraNotAvailableException.php @@ -0,0 +1,16 @@ +expectException(CoolRequestException::class); + $this->expectException(CollaboraNotAvailableException::class); $this->expectExceptionMessage("The configured Collabora Online server address must begin with 'http://' or 'https://'. Found 'httx://example.com'."); $discovery->getWopiClientURL();