From d14393fb3ddcdbc256b66f293f30d54804492e89 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Fri, 24 Aug 2018 12:58:17 +0100 Subject: [PATCH] MDL-63225 webservice: Return proper debuginfo on WS exceptions In order to make developers easy, we should give some clues about the type of the data generating exceptions. --- lib/externallib.php | 3 ++- lib/tests/externallib_test.php | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/externallib.php b/lib/externallib.php index 4b91e53c927..99ee7005d67 100644 --- a/lib/externallib.php +++ b/lib/externallib.php @@ -380,8 +380,9 @@ class external_api { return (bool)$response; } } + $responsetype = gettype($response); $debuginfo = 'Invalid external api response: the value is "' . $response . - '", the server was expecting "' . $description->type . '" type'; + '" of PHP type "' . $responsetype . '", the server was expecting "' . $description->type . '" type'; try { return validate_param($response, $description->type, $description->allownull, $debuginfo); } catch (invalid_parameter_exception $e) { diff --git a/lib/tests/externallib_test.php b/lib/tests/externallib_test.php index 5d449da04d0..d62c9bb37b2 100644 --- a/lib/tests/externallib_test.php +++ b/lib/tests/externallib_test.php @@ -232,6 +232,27 @@ class core_externallib_testcase extends advanced_testcase { $settings->set_filter($currentfilter); } + /** + * Test for clean_returnvalue() for testing that returns the PHP type. + */ + public function test_clean_returnvalue_return_php_type() { + + $returndesc = new external_single_structure( + array( + 'value' => new external_value(PARAM_RAW, 'Some text', VALUE_OPTIONAL, null, NULL_NOT_ALLOWED) + ) + ); + + // Check return type on exception because the external values does not allow NULL values. + $testdata = array('value' => null); + try { + $cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata); + } catch (moodle_exception $e) { + $this->assertInstanceOf('invalid_response_exception', $e); + $this->assertContains('of PHP type "NULL"', $e->debuginfo); + } + } + /** * Test for clean_returnvalue(). */