Skip to content

Commit

Permalink
MDL-54799 webservice: Handle exception in WS format ASAP
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Nov 29, 2019
1 parent 800563e commit 4ed6010
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/setup.php
Expand Up @@ -566,6 +566,11 @@
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);

Expand Down
17 changes: 17 additions & 0 deletions webservice/lib.php
Expand Up @@ -1753,3 +1753,20 @@ protected function service_class_method_body($function, $params) {
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;
}
19 changes: 14 additions & 5 deletions webservice/rest/locallib.php
Expand Up @@ -48,6 +48,19 @@ public function __construct($authmethod) {
$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:
Expand All @@ -64,11 +77,7 @@ protected function parse_request() {

// 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) {
Expand Down
12 changes: 12 additions & 0 deletions webservice/rest/server.php
Expand Up @@ -44,3 +44,15 @@
$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);
}
11 changes: 11 additions & 0 deletions webservice/soap/server.php
Expand Up @@ -43,3 +43,14 @@
$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);
}
12 changes: 12 additions & 0 deletions webservice/xmlrpc/server.php
Expand Up @@ -43,3 +43,15 @@
$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);
}

0 comments on commit 4ed6010

Please sign in to comment.