From c24332383b1bf0f1bd67dcb9a487ee0b4d81d167 Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Sat, 3 Oct 2020 14:59:13 -0400 Subject: [PATCH] Handle server unavailability better This prevents xml parsing errors when the server is not avialable. In a follow-up patch we will throw a 'server unavailable' error to better communicate the issue. --- classes/collabora.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/classes/collabora.php b/classes/collabora.php index 278fd6f..032b996 100644 --- a/classes/collabora.php +++ b/classes/collabora.php @@ -287,6 +287,11 @@ private function get_discovery_xml() { $url = rtrim($baseurl, '/').'/hosting/discovery'; $curl = new \curl(); $xml = $curl->get($url); + if ($curl->get_errno()) { + // $xml possibly contains some error message, which isn't a valid xml. + return ''; // TODO: throw exception to avoid causing secondary errors. + } + $cache->set($baseurl, $xml); } return $xml; @@ -299,8 +304,12 @@ private function get_discovery_xml() { * @return string */ private function get_url_from_mimetype($discoveryxml, $mimetype) { - $xml = new \SimpleXMLElement($discoveryxml); - $app = $xml->xpath("//app[@name='{$mimetype}']"); + $app = null; + if ($discoveryxml) { + $xml = new \SimpleXMLElement($discoveryxml); + $app = $xml->xpath("//app[@name='{$mimetype}']"); + } + if (!$app) { throw new \moodle_exception('unsupportedtype', 'mod_collabora', '', $mimetype); }