/** * Print an error page displaying an error message. New method - use this for new code. * * @param string $errorcode The name of the string from error.php to print * @param string $module name of module * @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. * @param object $a Extra words and phrases that might be required in the error string * @return terminates script, does not return! */ function print_error($errorcode, $module = 'error', $link = '', $a = NULL) { global $CFG, $UNITTEST; // If unittest running, throw exception instead if (!empty($UNITTEST->running)) { // Errors in unit test become exceptions, so you can unit test // code that might call error(). throw new moodle_exception($errorcode, $module, $link, $a); } if (empty($module) || $module == 'moodle' || $module == 'core') { $module = 'error'; } if (!isset($CFG->theme) or !isset($CFG->stylesheets)) { // error found before setup.php finished _print_early_error($errorcode, $module, $a); } else { _print_normal_error($errorcode, $module, $a, $link, debug_backtrace()); } }
/** * Default exception handler, uncought exceptions are equivalent to using print_error() */ function default_exception_handler($ex) { global $CFG; $backtrace = $ex->getTrace(); $place = array('file' => $ex->getFile(), 'line' => $ex->getLine(), 'exception' => get_class($ex)); array_unshift($backtrace, $place); $earlyerror = !isset($CFG->theme) || !isset($CFG->stylesheets); foreach ($backtrace as $stackframe) { if (isset($stackframe['function']) && $stackframe['function'] == 'print_header') { $earlyerror = true; break; } } if ($ex instanceof moodle_exception) { $errorcode = $ex->errorcode; $module = $ex->module; $a = $ex->a; $link = $ex->link; $debuginfo = $ex->debuginfo; } else { $errorcode = 'generalexceptionmessage'; $module = 'error'; $a = $ex->getMessage(); $link = ''; $debuginfo = null; } if ($earlyerror) { _print_early_error($errorcode, $module, $a, $backtrace, $debuginfo); } else { _print_normal_error($errorcode, $module, $a, $link, $backtrace, $debuginfo); } }