MDL-54799 webservice: Handle exception in WS format ASAP

This commit is contained in:
Pau Ferrer Ocaña 2019-09-17 09:25:41 +02:00
parent 800563e415
commit 4ed6010a61
6 changed files with 71 additions and 5 deletions

View File

@ -566,6 +566,11 @@ if (defined('BEHAT_SITE_RUNNING') && !defined('BEHAT_TEST') && !defined('BEHAT_U
set_error_handler('behat_error_handler', E_ALL | E_STRICT);
}
if (defined('WS_SERVER') && WS_SERVER) {
require_once($CFG->dirroot . '/webservice/lib.php');
set_exception_handler('early_ws_exception_handler');
}
// If there are any errors in the standard libraries we want to know!
error_reporting(E_ALL | E_STRICT);

View File

@ -1753,3 +1753,20 @@ EOD;
return $methodbody;
}
}
/**
* Early WS exception handler.
* It handles exceptions during setup and returns the Exception text in the WS format.
* If a raise function is found nothing is returned. Throws Exception otherwise.
*
* @param Exception $ex Raised exception.
* @throws Exception
*/
function early_ws_exception_handler(Exception $ex): void {
if (function_exists('raise_early_ws_exception')) {
raise_early_ws_exception($ex);
die;
}
throw $ex;
}

View File

@ -48,6 +48,19 @@ class webservice_rest_server extends webservice_base_server {
$this->wsname = 'rest';
}
/**
* Set the request format to.
*/
public function set_rest_format(): void {
// Get GET and POST parameters.
$methodvariables = array_merge($_GET, $_POST);
// Retrieve REST format parameter - 'xml' (default) or 'json'.
$restformatisset = isset($methodvariables['moodlewsrestformat'])
&& (($methodvariables['moodlewsrestformat'] == 'xml' || $methodvariables['moodlewsrestformat'] == 'json'));
$this->restformat = $restformatisset ? $methodvariables['moodlewsrestformat'] : 'xml';
}
/**
* This method parses the $_POST and $_GET superglobals and looks for
* the following information:
@ -64,11 +77,7 @@ class webservice_rest_server extends webservice_base_server {
// Get GET and POST parameters.
$methodvariables = array_merge($_GET, $_POST);
// Retrieve REST format parameter - 'xml' (default) or 'json'.
$restformatisset = isset($methodvariables['moodlewsrestformat'])
&& (($methodvariables['moodlewsrestformat'] == 'xml' || $methodvariables['moodlewsrestformat'] == 'json'));
$this->restformat = $restformatisset ? $methodvariables['moodlewsrestformat'] : 'xml';
$this->set_rest_format();
unset($methodvariables['moodlewsrestformat']);
if ($this->authmethod == WEBSERVICE_AUTHMETHOD_USERNAME) {

View File

@ -44,3 +44,15 @@ $server = new webservice_rest_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN);
$server->run();
die;
/**
* Raises Early WS Exception in REST format.
*
* @param Exception $ex Raised exception.
*/
function raise_early_ws_exception(Exception $ex): void {
global $CFG;
require_once("$CFG->dirroot/webservice/rest/locallib.php");
$server = new webservice_rest_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN);
$server->set_rest_format();
$server->exception_handler($ex);
}

View File

@ -43,3 +43,14 @@ $server = new webservice_soap_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN);
$server->run();
die;
/**
* Raises Early WS Exception in SOAP format.
*
* @param Exception $ex Raised exception.
*/
function raise_early_ws_exception(Exception $ex): void {
global $CFG;
require_once("$CFG->dirroot/webservice/soap/locallib.php");
$server = new webservice_soap_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN);
$server->exception_handler($ex);
}

View File

@ -43,3 +43,15 @@ $server = new webservice_xmlrpc_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN);
$server->run();
die;
/**
* Raises Early WS Exception in XMLRPC format.
*
* @param Exception $ex Raised exception.
*/
function raise_early_ws_exception(Exception $ex): void {
global $CFG;
require_once("$CFG->dirroot/webservice/xmlrpc/locallib.php");
$server = new webservice_xmlrpc_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN);
$server->exception_handler($ex);
}