/** * Returns detailed information about specified exception. * @param exception $ex * @return object */ function get_exception_info($ex) { global $CFG, $DB, $SESSION; 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 = ''; } // Append the error code to the debug info to make grepping and googling easier $debuginfo .= PHP_EOL . "Error code: {$errorcode}"; $backtrace = $ex->getTrace(); $place = array('file' => $ex->getFile(), 'line' => $ex->getLine(), 'exception' => get_class($ex)); array_unshift($backtrace, $place); // Be careful, no guarantee moodlelib.php is loaded. if (empty($module) || $module == 'moodle' || $module == 'core') { $module = 'error'; } // Search for the $errorcode's associated string // If not found, append the contents of $a to $debuginfo so helpful information isn't lost if (function_exists('get_string_manager')) { if (get_string_manager()->string_exists($errorcode, $module)) { $message = get_string($errorcode, $module, $a); } elseif ($module == 'error' && get_string_manager()->string_exists($errorcode, 'moodle')) { // Search in moodle file if error specified - needed for backwards compatibility $message = get_string($errorcode, 'moodle', $a); } else { $message = $module . '/' . $errorcode; $debuginfo .= PHP_EOL . '$a contents: ' . print_r($a, true); } } else { $message = $module . '/' . $errorcode; $debuginfo .= PHP_EOL . '$a contents: ' . print_r($a, true); } // Be careful, no guarantee weblib.php is loaded. if (function_exists('clean_text')) { $message = clean_text($message); } else { $message = htmlspecialchars($message); } if (!empty($CFG->errordocroot)) { $errordoclink = $CFG->errordocroot . '/en/'; } else { $errordoclink = get_docs_url(); } if ($module === 'error') { $modulelink = 'moodle'; } else { $modulelink = $module; } $moreinfourl = $errordoclink . 'error/' . $modulelink . '/' . $errorcode; if (empty($link)) { if (!empty($SESSION->fromurl)) { $link = $SESSION->fromurl; unset($SESSION->fromurl); } else { $link = $CFG->wwwroot . '/'; } } // when printing an error the continue button should never link offsite if (stripos($link, $CFG->wwwroot) === false && stripos($link, $CFG->httpswwwroot) === false) { $link = $CFG->wwwroot . '/'; } $info = new stdClass(); $info->message = $message; $info->errorcode = $errorcode; $info->backtrace = $backtrace; $info->link = $link; $info->moreinfourl = $moreinfourl; $info->a = $a; $info->debuginfo = $debuginfo; return $info; }
private function addExceptionToScore(\exception $exception) { list($file, $line) = $this->getBacktrace($exception->getTrace()); $this->score->addException($file, $this->class, $this->currentMethod, $line, $exception); return $this; }
/** * Returns detailed information about specified exception. * @param exception $ex * @return object */ function get_exception_info($ex) { global $CFG, $DB, $SESSION; 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 = ''; } // Append the error code to the debug info to make grepping and googling easier $debuginfo .= PHP_EOL . "Error code: {$errorcode}"; $backtrace = $ex->getTrace(); $place = array('file' => $ex->getFile(), 'line' => $ex->getLine(), 'exception' => get_class($ex)); array_unshift($backtrace, $place); // Be careful, no guarantee moodlelib.php is loaded. if (empty($module) || $module == 'moodle' || $module == 'core') { $module = 'error'; } // Search for the $errorcode's associated string // If not found, append the contents of $a to $debuginfo so helpful information isn't lost if (function_exists('get_string_manager')) { if (get_string_manager()->string_exists($errorcode, $module)) { $message = get_string($errorcode, $module, $a); } elseif ($module == 'error' && get_string_manager()->string_exists($errorcode, 'moodle')) { // Search in moodle file if error specified - needed for backwards compatibility $message = get_string($errorcode, 'moodle', $a); } else { $message = $module . '/' . $errorcode; $debuginfo .= PHP_EOL . '$a contents: ' . print_r($a, true); } } else { $message = $module . '/' . $errorcode; $debuginfo .= PHP_EOL . '$a contents: ' . print_r($a, true); } // Remove some absolute paths from message and debugging info. $searches = array(); $replaces = array(); $cfgnames = array('tempdir', 'cachedir', 'localcachedir', 'themedir', 'dataroot', 'dirroot'); foreach ($cfgnames as $cfgname) { if (property_exists($CFG, $cfgname)) { $searches[] = $CFG->{$cfgname}; $replaces[] = "[{$cfgname}]"; } } if (!empty($searches)) { $message = str_replace($searches, $replaces, $message); $debuginfo = str_replace($searches, $replaces, $debuginfo); } // Be careful, no guarantee weblib.php is loaded. if (function_exists('clean_text')) { $message = clean_text($message); } else { $message = htmlspecialchars($message); } if (!empty($CFG->errordocroot)) { $errordoclink = $CFG->errordocroot . '/en/'; } else { $errordoclink = get_docs_url(); } if ($module === 'error') { $modulelink = 'moodle'; } else { $modulelink = $module; } $moreinfourl = $errordoclink . 'error/' . $modulelink . '/' . $errorcode; if (empty($link)) { if (!empty($SESSION->fromurl)) { $link = $SESSION->fromurl; unset($SESSION->fromurl); } else { $link = $CFG->wwwroot . '/'; } } // When printing an error the continue button should never link offsite. // We cannot use clean_param() here as it is not guaranteed that it has been loaded yet. $httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot); if (stripos($link, $CFG->wwwroot) === 0) { // Internal HTTP, all good. } else { if (!empty($CFG->loginhttps) && stripos($link, $httpswwwroot) === 0) { // Internal HTTPS, all good. } else { // External link spotted! $link = $CFG->wwwroot . '/'; } } $info = new stdClass(); $info->message = $message; $info->errorcode = $errorcode; $info->backtrace = $backtrace; $info->link = $link; $info->moreinfourl = $moreinfourl; $info->a = $a; $info->debuginfo = $debuginfo; return $info; }
/** * Returns detailed information about specified exception. * @param exception $ex * @return object */ function get_exception_info($ex) { global $CFG, $DB, $SESSION; 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; } $backtrace = $ex->getTrace(); $place = array('file'=>$ex->getFile(), 'line'=>$ex->getLine(), 'exception'=>get_class($ex)); array_unshift($backtrace, $place); // Be careful, no guarantee moodlelib.php is loaded. if (empty($module) || $module == 'moodle' || $module == 'core') { $module = 'error'; } if (function_exists('get_string_manager')) { if (get_string_manager()->string_exists($errorcode, $module)) { $message = get_string($errorcode, $module, $a); } elseif ($module == 'error' && get_string_manager()->string_exists($errorcode, 'moodle')) { // Search in moodle file if error specified - needed for backwards compatibility $message = get_string($errorcode, 'moodle', $a); } else { $message = $module . '/' . $errorcode; } } else { $message = $module . '/' . $errorcode; } // Be careful, no guarantee weblib.php is loaded. if (function_exists('clean_text')) { $message = clean_text($message); } else { $message = htmlspecialchars($message); } if (!empty($CFG->errordocroot)) { $errordocroot = $CFG->errordocroot; } else if (!empty($CFG->docroot)) { $errordocroot = $CFG->docroot; } else { $errordocroot = 'http://docs.moodle.org'; } if ($module === 'error') { $modulelink = 'moodle'; } else { $modulelink = $module; } $moreinfourl = $errordocroot . '/en/error/' . $modulelink . '/' . $errorcode; if (empty($link)) { if (!empty($SESSION->fromurl)) { $link = $SESSION->fromurl; unset($SESSION->fromurl); } else { $link = $CFG->wwwroot .'/'; } } $info = new stdClass(); $info->message = $message; $info->errorcode = $errorcode; $info->backtrace = $backtrace; $info->link = $link; $info->moreinfourl = $moreinfourl; $info->a = $a; $info->debuginfo = $debuginfo; return $info; }
/** * Custom exception handler with backtrace * * @param exception $exception Thrown exception * * @return void */ function exceptionHandler($exception) { global $dPconfig; $time = date("Y-m-d H:i:s"); // User information $user_id = null; $user_view = ""; if (class_exists("CAppUI", false) && CAppUI::$user) { $user = CAppUI::$user; if ($user->_id) { $user_id = $user->_id; $user_view = $user->_view; } } // Server IP $server_ip = isset($_SERVER["SERVER_ADDR"]) ? $_SERVER["SERVER_ADDR"] : null; $file = mbRelativePath($exception->getFile()); $line = $exception->getLine(); $type = "exception"; $text = $exception->getMessage(); // Stacktrace $contexts = $exception->getTrace(); foreach ($contexts as &$ctx) { unset($ctx['args']); } // Might noy be ready at the time error is thrown $session = isset($_SESSION) ? $_SESSION : array(); unset($session['AppUI']); unset($session['dPcompteRendu']['templateManager']); $_all_params = array("GET" => $_GET, "POST" => $_POST, "SESSION" => $session); filterInput($_all_params); // CApp might not be ready yet as of early error handling $request_uid = null; if (class_exists("CApp", false)) { $request_uid = CApp::getRequestUID(); CApp::$performance[CError::$_categories["exception"]]++; } $build_output = ini_get("display_errors"); $save_to_file = false; $data = array("stacktrace" => $contexts, "param_GET" => $_all_params["GET"], "param_POST" => $_all_params["POST"], "session_data" => $_all_params["SESSION"]); if (@$dPconfig["error_logs_in_db"] && class_exists("CErrorLog")) { try { CErrorLog::insert($user_id, $server_ip, $time, $request_uid, $type, $text, $file, $line, $data); } catch (Exception $e) { $build_output = true; $save_to_file = true; } } else { $build_output = true; $save_to_file = true; } if ($build_output) { $hash = md5(serialize($contexts)); $html_class = "big-warning"; $log = "\n\n<div class='{$html_class}' title='{$hash}'>"; if ($user_id) { $log .= "\n<strong>User: </strong>{$user_view} ({$user_id})"; } $file = CError::openInIDE($file, $line); $log .= <<<HTML <strong>Time: </strong>{$time} <strong>Type: </strong>{$type} <strong>Text: </strong>{$text} <strong>File: </strong>{$file} <strong>Line: </strong>{$line} HTML; foreach ($_all_params as $_type => $_params) { $log .= print_infos($_all_params[$_type], $_type); } foreach ($contexts as $context) { $function = isset($context["class"]) ? $context["class"] . ":" : ""; $function .= $context["function"] . "()"; $log .= "\n<strong>Function: </strong> {$function}"; if (isset($context["file"])) { $log .= "\n<strong>File: </strong>" . CError::openInIDE($context["file"], isset($context["line"]) ? $context["line"] : null); } if (isset($context["line"])) { $log .= "\n<strong>Line: </strong>" . $context["line"]; } $log .= "<br />"; } $log .= "</div>"; if ($save_to_file) { file_put_contents(LOG_PATH, $log, FILE_APPEND); } if (ini_get("display_errors")) { echo $log; } } }
/** * Function handle_exceptions * * * @param exception $e * @return void */ function handle_exceptions(exception $e) { global $msg; $trace = $e->getTrace(); file_put_contents(_LOG_PATH_ . 'admin.exception.log', print_r($trace, true), FILE_APPEND); $msg .= $e->getMessage(); return 'logout'; }
/** * Displays an exception in HTML, and exists. Includes the exception * trace in an HTML comment, and a readable error string along with the * exception message. * @param exception $e Exception */ public static function handle_exception($e) { // Display actual trace in HTML comment. There shouldn't be any // security-sensitive information in the trace, so this can be // displayed even on live server (I hope). if (debugging('', DEBUG_DEVELOPER)) { global $CFG; print "<pre class='forumng-stacktrace'>"; print htmlspecialchars(str_replace($CFG->dirroot, '', $e->getTraceAsString())); print "</pre>"; } else { print "<!--\n"; print $e->getTraceAsString(); // Not escaped, I think this is correct... print "\n-->"; } // Make a short version of the trace string for log $minitrace = self::get_minitrace_part($e->getFile(), $e->getLine()); foreach ($e->getTrace() as $entry) { $minitrace .= ' ' . self::get_minitrace_part($entry['file'], $entry['line']); } $minitrace = shorten_text($minitrace, 120, true); $message = shorten_text($e->getMessage(), 120, true); global $FULLME, $USER, $CFG; $url = str_replace($CFG->wwwroot . '/mod/forumng/', '', $FULLME); add_to_log(0, 'forumng', 'error', $url, "{$message} / {$minitrace}", 0, $USER->id); // Error to user with just the message print_error('error_exception', 'forumng', '', $e->getMessage()); }
/** * Handle with exceptions * * @param exception $e */ public function handleException($e) { $msg = ''; $trace = $e->getTrace(); ksort($trace); foreach ($trace as $error) { if (isset($error['function']) && isset($error['file'])) { $msg .= $error['file'] . ' (' . $error['line'] . ') '; if (isset($error['function']) && is_string($error['function'])) { $msg .= (isset($error['class']) ? $error['class'] . $error['type'] : '') . $error['function'] . '()'; } $msg .= '<br />'; } } echo <<<EOT <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Akami Framework</title> </head> <body> <style> html, body { height: 100%; padding: 0; margin: 0; } body { width: 100%; display: table; background: #16938A; color: #333; font-size: 14px; line-height: 1.825; font-family: "Lucida Grande", Helvetica, Arial, "Microsoft YaHei", FreeSans, Arimo, "Droid Sans","wenquanyi micro hei","Hiragino Sans GB", "Hiragino Sans GB W3", Arial, sans-serif } .box { display: table-cell; vertical-align: middle; } .box .container { background: #fff; width: 500px; margin: 0 auto; padding: 2em; box-shadow: 0 2px 8px rgba(0, 0, 0, .2); -webkit-box-sizing: border-box; box-sizing: border-box; } header { color: #999; display: block; margin-bottom: 1em; } .bold { color: #222; font-weight: bold; } p { color: #222; } .message { color: #777; font-size: 12px; margin-top: 1em; margin-bottom: 0; } </style> <div class="box"> <div class="container"> <header> <span class="bold">Message Reminder</span> - {$e->getMessage()} </header> <p class="file"><span class="bold">Location: </span> {$e->getfile()} <i>({$e->getLine()})</i></p> <p class="message">{$msg}</p> </div> </div> </body> </html> EOT; }