Skip to content

Commit

Permalink
Issue CollaboraOnline#56: Rename the exception to CollaboraNotAvailab…
Browse files Browse the repository at this point in the history
…leException.
  • Loading branch information
donquixote committed Nov 21, 2024
1 parent 14217f9 commit 18482de
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/Controller/ViewerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
]);
Expand Down
16 changes: 8 additions & 8 deletions src/Cool/CollaboraConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
));
Expand All @@ -82,15 +82,15 @@ 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 {
$discovery_url = $this->getWopiClientServerBaseUrl() . '/hosting/discovery';

$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']);

Expand All @@ -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,
);
Expand Down
10 changes: 5 additions & 5 deletions src/Cool/CollaboraDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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.');
}

/**
Expand All @@ -58,15 +58,15 @@ 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 {
$xml = $this->collaboraConnection->getDiscoveryXml();

$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;
Expand Down
16 changes: 16 additions & 0 deletions src/Exception/CollaboraNotAvailableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Drupal\collabora_online\Exception;

/**
* Collabora is not available.
*
* The reason could be:
* - The configuration for this module is empty or invalid.
* - The Collabora service is not responding, or is not behaving as expected.
*/
class CollaboraNotAvailableException extends \Exception {

}
12 changes: 0 additions & 12 deletions src/Exception/CoolRequestException.php

This file was deleted.

4 changes: 2 additions & 2 deletions tests/src/ExistingSite/FetchClientUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Drupal\Tests\collabora_online\ExistingSite;

use Drupal\collabora_online\Cool\CollaboraDiscovery;
use Drupal\collabora_online\Exception\CoolRequestException;
use Drupal\collabora_online\Exception\CollaboraNotAvailableException;
use weitzman\DrupalTestTraits\ExistingSiteBase;

/**
Expand Down Expand Up @@ -39,7 +39,7 @@ public function testFetchClientUrlWithMisconfiguration(): void {
/** @var \Drupal\collabora_online\Cool\CollaboraDiscovery $discovery */
$discovery = \Drupal::service(CollaboraDiscovery::class);

$this->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();
Expand Down

0 comments on commit 18482de

Please sign in to comment.