/** * 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); } }
function upgrade_handle_exception($ex, $plugin = null) { global $CFG; if ($ex instanceof moodle_exception) { $details = get_string($ex->errorcode, $ex->module, $ex->a) . "<br />debugging:" . $ex->debuginfo; } else { $details = get_string('generalexceptionmessage', 'error', $ex->getMessage()); } $info = "Exception: " . get_class($ex); $backtrace = $ex->getTrace(); $place = array('file' => $ex->getFile(), 'line' => $ex->getLine(), 'exception' => get_class($ex)); array_unshift($backtrace, $place); /// first log upgrade error upgrade_log(UPGRADE_LOG_ERROR, $plugin, $info, $details, $backtrace); // always turn on debugging - admins need to know what is going on $CFG->debug = DEBUG_DEVELOPER; // now print the exception info as usually if ($ex instanceof moodle_exception) { _print_normal_error($ex->errorcode, $ex->module, $ex->a, $ex->link, $backtrace, $ex->debuginfo); } else { _print_normal_error('generalexceptionmessage', 'error', $ex->getMessage(), '', $backtrace); } die; // not reached }
/** * Print an error page displaying an error message. * Old method, don't call directly in new code - use print_error instead. * * @param string $message The message to display to the user about the error. * @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. * @return terminates script, does not return! */ function error($message, $link = '') { global $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('notlocalisederrormessage', 'error', $link, $message); } _print_normal_error('notlocalisederrormessage', 'error', $message, $link, debug_backtrace(), null, true); // show debug warning }