getMessage()); $msg = 'An unforeseen error has occured. This is most likely a bug somewhere.'; if ($plugin) $msg .= ' It might be a problem in the ' . $plugin . ' plugin.'; $logged = self::logException($e) ? 'More info has been written to the DokuWiki _error.log' : $e->getFile() . ':' . $e->getLine(); echo << $title

$title

$msg

$logged

EOT; } /** * Convenience method to display an error message for the given Exception * * @param \Throwable $e * @param string $intro */ public static function showExceptionMsg($e, $intro = 'Error!') { $msg = hsc($intro) . '
' . hsc(get_class($e) . ': ' . $e->getMessage()); if (self::logException($e)) $msg .= '
More info is available in the _error.log'; msg($msg, -1); } /** * Last resort to handle fatal errors that still can't be caught */ public static function fatalShutdown() { $error = error_get_last(); // Check if it's a core/fatal error, otherwise it's a normal shutdown if ( $error !== null && in_array( $error['type'], [ E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, ] ) ) { self::fatalException( new FatalException($error['message'], 0, $error['type'], $error['file'], $error['line']) ); } } /** * Log the given exception to the error log * * @param \Throwable $e * @return bool false if the logging failed */ public static function logException($e) { global $conf; $log = join("\t", [ gmdate('c'), get_class($e), $e->getFile() . '(' . $e->getLine() . ')', $e->getMessage(), ]) . "\n"; $log .= $e->getTraceAsString() . "\n"; return io_saveFile($conf['cachedir'] . '/_error.log', $log, true); } /** * Checks the the stacktrace for plugin files * * @param \Throwable $e * @return false|string */ protected static function guessPlugin($e) { if (preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $e->getFile()), $match)) { return $match[1]; } foreach ($e->getTrace() as $line) { if ( isset($line['class']) && preg_match('/\w+?_plugin_(\w+)/', $line['class'], $match) ) { return $match[1]; } if ( isset($line['file']) && preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $line['file']), $match) ) { return $match[1]; } } return false; } }