|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +/** |
| 18 | + * Base Moodle Exception class |
| 19 | + * |
| 20 | + * Although this class is defined here, you cannot throw a moodle_exception until |
| 21 | + * after moodlelib.php has been included (which will happen very soon). |
| 22 | + * |
| 23 | + * @package core |
| 24 | + * @subpackage lib |
| 25 | + * @copyright 2008 Petr Skoda {@link http://skodak.org} |
| 26 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 27 | + */ |
| 28 | +class moodle_exception extends \Exception { |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string The name of the string from error.php to print |
| 32 | + */ |
| 33 | + public $errorcode; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var string The name of module |
| 37 | + */ |
| 38 | + public $module; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var mixed Extra words and phrases that might be required in the error string |
| 42 | + */ |
| 43 | + public $a; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var string The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page. |
| 47 | + */ |
| 48 | + public $link; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var string Optional information to aid the debugging process |
| 52 | + */ |
| 53 | + public $debuginfo; |
| 54 | + |
| 55 | + /** |
| 56 | + * Constructor |
| 57 | + * @param string $errorcode The name of the string from error.php to print |
| 58 | + * @param string $module name of module |
| 59 | + * @param string $link The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page. |
| 60 | + * @param mixed $a Extra words and phrases that might be required in the error string |
| 61 | + * @param string $debuginfo optional debugging information |
| 62 | + */ |
| 63 | + function __construct($errorcode, $module='', $link='', $a=NULL, $debuginfo=null) { |
| 64 | + global $CFG; |
| 65 | + |
| 66 | + if (empty($module) || $module == 'moodle' || $module == 'core') { |
| 67 | + $module = 'error'; |
| 68 | + } |
| 69 | + |
| 70 | + $this->errorcode = $errorcode; |
| 71 | + $this->module = $module; |
| 72 | + $this->link = $link; |
| 73 | + $this->a = $a; |
| 74 | + $this->debuginfo = is_null($debuginfo) ? null : (string)$debuginfo; |
| 75 | + |
| 76 | + if (get_string_manager()->string_exists($errorcode, $module)) { |
| 77 | + $message = get_string($errorcode, $module, $a); |
| 78 | + $haserrorstring = true; |
| 79 | + } else { |
| 80 | + $message = $module . '/' . $errorcode; |
| 81 | + $haserrorstring = false; |
| 82 | + } |
| 83 | + |
| 84 | + $isinphpunittest = (defined('PHPUNIT_TEST') && PHPUNIT_TEST); |
| 85 | + $hasdebugdeveloper = ( |
| 86 | + isset($CFG->debugdisplay) && |
| 87 | + isset($CFG->debug) && |
| 88 | + $CFG->debugdisplay && |
| 89 | + $CFG->debug === DEBUG_DEVELOPER |
| 90 | + ); |
| 91 | + |
| 92 | + if ($debuginfo) { |
| 93 | + if ($isinphpunittest || $hasdebugdeveloper) { |
| 94 | + $message = "$message ($debuginfo)"; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (!$haserrorstring and $isinphpunittest) { |
| 99 | + // Append the contents of $a to $debuginfo so helpful information isn't lost. |
| 100 | + // This emulates what {@link get_exception_info()} does. Unfortunately that |
| 101 | + // function is not used by phpunit. |
| 102 | + $message .= PHP_EOL.'$a contents: '.print_r($a, true); |
| 103 | + } |
| 104 | + |
| 105 | + parent::__construct($message, 0); |
| 106 | + } |
| 107 | +} |
0 commit comments