Skip to content

Commit

Permalink
MDL-63225 webservice: Return proper debuginfo on WS exceptions
Browse files Browse the repository at this point in the history
In order to make developers easy, we should give some clues about the type of the data generating exceptions.
  • Loading branch information
jleyva committed Oct 2, 2018
1 parent 5211af1 commit 86196f9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ public static function clean_returnvalue(external_description $description, $res
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) {
Expand Down
21 changes: 21 additions & 0 deletions lib/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,27 @@ public function test_external_format_string() {
$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().
*/
Expand Down

0 comments on commit 86196f9

Please sign in to comment.