diff --git a/lang/en_utf8/webservice.php b/lang/en_utf8/webservice.php index 9948c719e40..02f33d9424b 100644 --- a/lang/en_utf8/webservice.php +++ b/lang/en_utf8/webservice.php @@ -59,6 +59,7 @@ $string['functions'] = 'Functions'; $string['generalstructure'] = 'General structure'; $string['httpswarning'] = 'Token strings are only displayed if your connection is secured (https)'; $string['information'] = 'Information'; +$string['invalidextparam'] = 'Invalid external api parameter: $a'; $string['invalidiptoken'] = 'Invalid token - your IP is not supported'; $string['invalidtimedtoken'] = 'Invalid token - token expired'; $string['invalidtoken'] = 'Invalid token - token not found'; diff --git a/lib/externallib.php b/lib/externallib.php index 65d72325fae..055a8cd2bc2 100644 --- a/lib/externallib.php +++ b/lib/externallib.php @@ -168,11 +168,19 @@ class external_api { } if ($subdesc instanceof external_value) { if ($subdesc->required == VALUE_DEFAULT) { - $result[$key] = self::validate_parameters($subdesc, $subdesc->default); + try { + $result[$key] = self::validate_parameters($subdesc, $subdesc->default); + } catch (invalid_parameter_exception $e) { + throw new webservice_parameter_exception('invalidextparam',$key); + } } } } else { - $result[$key] = self::validate_parameters($subdesc, $params[$key]); + try { + $result[$key] = self::validate_parameters($subdesc, $params[$key]); + } catch (invalid_parameter_exception $e) { + throw new webservice_parameter_exception('invalidextparam',$key); + } } unset($params[$key]); } diff --git a/lib/setuplib.php b/lib/setuplib.php index 2aa157b3f19..8170784180d 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -91,6 +91,23 @@ class moodle_exception extends Exception { } } +/** + * Web service parameter exception class + * + * This exception must be thrown to the web service client when a web service parameter is invalid + * The error string is gotten from webservice.php + */ +class webservice_parameter_exception extends moodle_exception { + /** + * Constructor + * @param string $errorcode The name of the string from webservice.php to print + * @param string $a The name of the parameter + */ + function __construct($errorcode=null, $a = '') { + parent::__construct($errorcode, 'webservice', '', $a, null); + } +} + /** * Exceptions indicating user does not have permissions to do something * and the execution can not continue.