From 6e9e73aba09ee4f749af37a3a1d4743db0fcafaf Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Thu, 31 Aug 2023 14:48:58 +0100 Subject: [PATCH] MDL-79205 webservice: gracefully handle invalid functions in docs. --- admin/webservice/documentation.php | 20 +++++++++++++------- lib/external/classes/external_api.php | 1 + 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/admin/webservice/documentation.php b/admin/webservice/documentation.php index 6ba56b2f3e6..eb064b02069 100644 --- a/admin/webservice/documentation.php +++ b/admin/webservice/documentation.php @@ -32,13 +32,6 @@ require_once($CFG->dirroot . '/webservice/lib.php'); admin_externalpage_setup('webservicedocumentation'); -// get all the function descriptions -$functions = $DB->get_records('external_functions', array(), 'name'); -$functiondescs = array(); -foreach ($functions as $function) { - $functiondescs[$function->name] = external_api::external_function_info($function); -} - // TODO: MDL-76078 - Incorrect inter-communication, core cannot have plugin dependencies like this. //display the documentation for all documented protocols, @@ -53,6 +46,19 @@ $printableformat = optional_param('print', false, PARAM_BOOL); /// OUTPUT echo $OUTPUT->header(); +// Get all the function descriptions. +$functions = $DB->get_records('external_functions', [], 'name'); +$functiondescs = []; +foreach ($functions as $function) { + + // Skip invalid or otherwise incorrectly defined functions, otherwise the entire page is rendered inaccessible. + try { + $functiondescs[$function->name] = external_api::external_function_info($function); + } catch (Throwable $exception) { + echo $OUTPUT->notification($exception->getMessage(), \core\output\notification::NOTIFY_ERROR); + } +} + $renderer = $PAGE->get_renderer('core', 'webservice'); echo $renderer->documentation_html($functiondescs, $printableformat, $protocols, array(), $PAGE->url); diff --git a/lib/external/classes/external_api.php b/lib/external/classes/external_api.php index c770a80ab71..365fde48220 100644 --- a/lib/external/classes/external_api.php +++ b/lib/external/classes/external_api.php @@ -46,6 +46,7 @@ class external_api { * @param int $strictness IGNORE_MISSING means compatible mode, false returned if record not found, debug message if more found; * MUST_EXIST means throw exception if no record or multiple records found * @return \stdClass|bool description or false if not found or exception thrown + * @throws coding_exception for any property and/or method that is missing or invalid * @since Moodle 2.0 */ public static function external_function_info($function, $strictness = MUST_EXIST) { -- 2.43.0