Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core\exception;
18
 
19
/**
20
 * Base Moodle Exception class
21
 *
22
 * Although this class is defined here, you cannot throw a moodle_exception until
23
 * after moodlelib.php has been included (which will happen very soon).
24
 *
25
 * @package    core
26
 * @subpackage exception
27
 * @copyright  2008 Petr Skoda  {@link http://skodak.org}
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class moodle_exception extends \Exception {
31
    /** @var string The name of the string from error.php to print */
32
    public $errorcode;
33
 
34
    /** @var string The name of module */
35
    public $module;
36
 
37
    /** @var mixed Extra words and phrases that might be required in the error string */
38
    public $a;
39
 
40
    /**
41
     * 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.
42
     *
43
     * @var string
44
     */
45
    public $link;
46
 
47
    /** @var string Optional information to aid the debugging process */
48
    public $debuginfo;
49
 
50
    /**
51
     * Constructor
52
     * @param string $errorcode The name of the string from error.php to print
53
     * @param string $module name of module
54
     * @param string $link The url where the user will be prompted to continue.
55
     * If no url is provided the user will be directed to the site index page.
56
     * @param mixed $a Extra words and phrases that might be required in the error string
57
     * @param string $debuginfo optional debugging information
58
     */
59
    public function __construct($errorcode, $module = '', $link = '', $a = null, $debuginfo = null) {
60
        global $CFG;
61
 
62
        if (empty($module) || $module == 'moodle' || $module == 'core') {
63
            $module = 'error';
64
        }
65
 
66
        $this->errorcode = $errorcode;
67
        $this->module    = $module;
68
        $this->link      = $link;
69
        $this->a         = $a;
70
        $this->debuginfo = is_null($debuginfo) ? null : (string)$debuginfo;
71
 
72
        if (get_string_manager()->string_exists($errorcode, $module)) {
73
            $message = get_string($errorcode, $module, $a);
74
            $haserrorstring = true;
75
        } else {
76
            $message = $module . '/' . $errorcode;
77
            $haserrorstring = false;
78
        }
79
 
80
        $isinphpunittest = (defined('PHPUNIT_TEST') && PHPUNIT_TEST);
81
        $hasdebugdeveloper = (
82
            isset($CFG->debugdisplay) &&
83
            isset($CFG->debug) &&
84
            $CFG->debugdisplay &&
85
            $CFG->debug === DEBUG_DEVELOPER
86
        );
87
 
88
        if ($debuginfo) {
89
            if ($isinphpunittest || $hasdebugdeveloper) {
90
                $message = "$message ($debuginfo)";
91
            }
92
        }
93
 
94
        if (!$haserrorstring && $isinphpunittest) {
95
            // Append the contents of $a to $debuginfo so helpful information isn't lost.
96
            // This emulates what {@link get_exception_info()} does. Unfortunately that
97
            // function is not used by phpunit.
98
            $message .= PHP_EOL . '$a contents: ' . print_r($a, true); // phpcs:ignore moodle.PHP.ForbiddenFunctions.Found
99
        }
100
 
101
        parent::__construct($message, 0);
102
    }
103
}
104
 
105
// Alias this class to the old name.
106
// This file will be autoloaded by the legacyclasses autoload system.
107
// In future all uses of this class will be corrected and the legacy references will be removed.
108
class_alias(moodle_exception::class, \moodle_exception::class);