-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'MDL-51571_ltiErrorHandler' of https://github.com/moodle…
- Loading branch information
Showing
6 changed files
with
393 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Exception handler for LTI services | ||
* | ||
* @package mod_lti | ||
* @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com) | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace mod_lti; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
require_once(__DIR__.'/../locallib.php'); | ||
require_once(__DIR__.'/../servicelib.php'); | ||
|
||
/** | ||
* Handles exceptions when handling incoming LTI messages. | ||
* | ||
* Ensures that LTI always returns a XML message that can be consumed by the caller. | ||
* | ||
* @package mod_lti | ||
* @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com) | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class service_exception_handler { | ||
/** | ||
* Enable error response logging. | ||
* | ||
* @var bool | ||
*/ | ||
protected $log = false; | ||
|
||
/** | ||
* The LTI service message ID, if known. | ||
* | ||
* @var string | ||
*/ | ||
protected $id = ''; | ||
|
||
/** | ||
* The LTI service message type, if known. | ||
* | ||
* @var string | ||
*/ | ||
protected $type = 'unknownRequest'; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param boolean $log Enable error response logging. | ||
*/ | ||
public function __construct($log) { | ||
$this->log = $log; | ||
} | ||
|
||
/** | ||
* Set the LTI message ID being handled. | ||
* | ||
* @param string $id | ||
*/ | ||
public function set_message_id($id) { | ||
if (!empty($id)) { | ||
$this->id = $id; | ||
} | ||
} | ||
|
||
/** | ||
* Set the LTI message type being handled. | ||
* | ||
* @param string $type | ||
*/ | ||
public function set_message_type($type) { | ||
if (!empty($type)) { | ||
$this->type = $type; | ||
} | ||
} | ||
|
||
/** | ||
* Echo an exception message encapsulated in XML | ||
* | ||
* @param \Exception $exception The exception that was thrown | ||
*/ | ||
public function handle(\Exception $exception) { | ||
$message = $exception->getMessage(); | ||
|
||
// Add the exception backtrace for developers. | ||
if (debugging('', DEBUG_DEVELOPER)) { | ||
$message .= "\n".format_backtrace(get_exception_info($exception)->backtrace, true); | ||
} | ||
|
||
// Switch to response. | ||
$type = str_replace('Request', 'Response', $this->type); | ||
|
||
// Build the appropriate xml. | ||
$response = lti_get_response_xml('failure', $message, $this->id, $type); | ||
|
||
$xml = $response->asXML(); | ||
|
||
// Log the request if necessary. | ||
if ($this->log) { | ||
lti_log_response($xml, $exception); | ||
} | ||
|
||
echo $xml; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
// This file is part of Moodle - http://moodle.org/ | ||
// | ||
// Moodle is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Moodle is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Tests Exception handler for LTI services | ||
* | ||
* @package mod_lti | ||
* @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com) | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
use mod_lti\service_exception_handler; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
/** | ||
* Tests Exception handler for LTI services | ||
* | ||
* @package mod_lti | ||
* @copyright Copyright (c) 2015 Moodlerooms Inc. (http://www.moodlerooms.com) | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class mod_lti_service_exception_handler_testcase extends advanced_testcase { | ||
/** | ||
* Testing service error handling. | ||
*/ | ||
public function test_handle() { | ||
$handler = new service_exception_handler(false); | ||
$handler->set_message_id('123'); | ||
$handler->set_message_type('testRequest'); | ||
$handler->handle(new Exception('Error happened')); | ||
|
||
$this->expectOutputRegex('/imsx_codeMajor>failure/'); | ||
$this->expectOutputRegex('/imsx_description>Error happened/'); | ||
$this->expectOutputRegex('/imsx_messageRefIdentifier>123/'); | ||
$this->expectOutputRegex('/imsx_operationRefIdentifier>testRequest/'); | ||
$this->expectOutputRegex('/imsx_POXBody><testResponse/'); | ||
} | ||
|
||
/** | ||
* Testing service error handling when message ID and type are not known yet. | ||
*/ | ||
public function test_handle_early_error() { | ||
$handler = new service_exception_handler(false); | ||
$handler->handle(new Exception('Error happened')); | ||
|
||
$this->expectOutputRegex('/imsx_codeMajor>failure/'); | ||
$this->expectOutputRegex('/imsx_description>Error happened/'); | ||
$this->expectOutputRegex('/imsx_messageRefIdentifier\/>/'); | ||
$this->expectOutputRegex('/imsx_operationRefIdentifier>unknownRequest/'); | ||
$this->expectOutputRegex('/imsx_POXBody><unknownResponse/'); | ||
} | ||
|
||
/** | ||
* Testing that a log file is generated when logging is turned on. | ||
*/ | ||
public function test_handle_log() { | ||
global $CFG; | ||
|
||
$this->resetAfterTest(); | ||
|
||
$handler = new service_exception_handler(true); | ||
|
||
ob_start(); | ||
$handler->handle(new Exception('Error happened')); | ||
ob_end_clean(); | ||
|
||
$this->assertTrue(is_dir($CFG->dataroot.'/temp/mod_lti')); | ||
$files = glob($CFG->dataroot.'/temp/mod_lti/mod_lti_response*'); | ||
$this->assertEquals(1, count($files)); | ||
} | ||
} |
Oops, something went wrong.