xref: /dokuwiki/inc/ErrorHandler.php (revision 0ecde6ce23ad8ee6132797de11e004e7458a83fd)
1642e976cSAndreas Gohr<?php
2642e976cSAndreas Gohr
3642e976cSAndreas Gohrnamespace dokuwiki;
4642e976cSAndreas Gohr
5dbe0790dSAndreas Gohruse dokuwiki\Exception\FatalException;
6dbe0790dSAndreas Gohr
7dbe0790dSAndreas Gohr/**
8dbe0790dSAndreas Gohr * Manage the global handling of errors and exceptions
9dbe0790dSAndreas Gohr *
10dbe0790dSAndreas Gohr * Developer may use this to log and display exceptions themselves
11dbe0790dSAndreas Gohr */
12642e976cSAndreas Gohrclass ErrorHandler
13642e976cSAndreas Gohr{
14642e976cSAndreas Gohr    /**
15642e976cSAndreas Gohr     * Register the default error handling
16642e976cSAndreas Gohr     */
17642e976cSAndreas Gohr    public static function register()
18642e976cSAndreas Gohr    {
19642e976cSAndreas Gohr        if (!defined('DOKU_UNITTEST')) {
20642e976cSAndreas Gohr            set_exception_handler([ErrorHandler::class, 'fatalException']);
21cb4cefebSAndreas Gohr            register_shutdown_function([ErrorHandler::class, 'fatalShutdown']);
22642e976cSAndreas Gohr        }
23642e976cSAndreas Gohr    }
24642e976cSAndreas Gohr
25642e976cSAndreas Gohr    /**
26642e976cSAndreas Gohr     * Default Exception handler to show a nice user message before dieing
27642e976cSAndreas Gohr     *
28642e976cSAndreas Gohr     * The exception is logged to the error log
29642e976cSAndreas Gohr     *
30642e976cSAndreas Gohr     * @param \Throwable $e
31642e976cSAndreas Gohr     */
32642e976cSAndreas Gohr    public static function fatalException($e)
33642e976cSAndreas Gohr    {
347e0b27f2SAndreas Gohr        $plugin = self::guessPlugin($e);
35642e976cSAndreas Gohr        $title = hsc(get_class($e) . ': ' . $e->getMessage());
36642e976cSAndreas Gohr        $msg = 'An unforeseen error has occured. This is most likely a bug somewhere.';
377e0b27f2SAndreas Gohr        if ($plugin) $msg .= ' It might be a problem in the ' . $plugin . ' plugin.';
38642e976cSAndreas Gohr        $logged = self::logException($e)
39*0ecde6ceSAndreas Gohr            ? 'More info has been written to the DokuWiki error log.'
40642e976cSAndreas Gohr            : $e->getFile() . ':' . $e->getLine();
41642e976cSAndreas Gohr
42642e976cSAndreas Gohr        echo <<<EOT
43642e976cSAndreas Gohr<!DOCTYPE html>
44642e976cSAndreas Gohr<html>
45642e976cSAndreas Gohr<head><title>$title</title></head>
46642e976cSAndreas Gohr<body style="font-family: Arial, sans-serif">
47642e976cSAndreas Gohr    <div style="width:60%; margin: auto; background-color: #fcc;
48642e976cSAndreas Gohr                border: 1px solid #faa; padding: 0.5em 1em;">
49642e976cSAndreas Gohr        <h1 style="font-size: 120%">$title</h1>
50642e976cSAndreas Gohr        <p>$msg</p>
51642e976cSAndreas Gohr        <p>$logged</p>
52642e976cSAndreas Gohr    </div>
53642e976cSAndreas Gohr</body>
54642e976cSAndreas Gohr</html>
55642e976cSAndreas GohrEOT;
56642e976cSAndreas Gohr    }
57642e976cSAndreas Gohr
58642e976cSAndreas Gohr    /**
59642e976cSAndreas Gohr     * Convenience method to display an error message for the given Exception
60642e976cSAndreas Gohr     *
61642e976cSAndreas Gohr     * @param \Throwable $e
62642e976cSAndreas Gohr     * @param string $intro
63642e976cSAndreas Gohr     */
64642e976cSAndreas Gohr    public static function showExceptionMsg($e, $intro = 'Error!')
65642e976cSAndreas Gohr    {
66ffa84f81SAndreas Gohr        $msg = hsc($intro) . '<br />' . hsc(get_class($e) . ': ' . $e->getMessage());
67*0ecde6ceSAndreas Gohr        if (self::logException($e)) $msg .= '<br />More info is available in the error log.';
68ffa84f81SAndreas Gohr        msg($msg, -1);
69642e976cSAndreas Gohr    }
70642e976cSAndreas Gohr
71642e976cSAndreas Gohr    /**
72dbe0790dSAndreas Gohr     * Last resort to handle fatal errors that still can't be caught
73cb4cefebSAndreas Gohr     */
74cb4cefebSAndreas Gohr    public static function fatalShutdown()
75cb4cefebSAndreas Gohr    {
76cb4cefebSAndreas Gohr        $error = error_get_last();
77cb4cefebSAndreas Gohr        // Check if it's a core/fatal error, otherwise it's a normal shutdown
78cb4cefebSAndreas Gohr        if (
79cb4cefebSAndreas Gohr            $error !== null &&
80cb4cefebSAndreas Gohr            in_array(
81cb4cefebSAndreas Gohr                $error['type'],
82cb4cefebSAndreas Gohr                [
83cb4cefebSAndreas Gohr                    E_ERROR,
84cb4cefebSAndreas Gohr                    E_CORE_ERROR,
85cb4cefebSAndreas Gohr                    E_COMPILE_ERROR,
86cb4cefebSAndreas Gohr                ]
87cb4cefebSAndreas Gohr            )
88cb4cefebSAndreas Gohr        ) {
89cb4cefebSAndreas Gohr            self::fatalException(
90dbe0790dSAndreas Gohr                new FatalException($error['message'], 0, $error['type'], $error['file'], $error['line'])
91cb4cefebSAndreas Gohr            );
92cb4cefebSAndreas Gohr        }
93cb4cefebSAndreas Gohr    }
94cb4cefebSAndreas Gohr
95cb4cefebSAndreas Gohr    /**
96642e976cSAndreas Gohr     * Log the given exception to the error log
97642e976cSAndreas Gohr     *
98642e976cSAndreas Gohr     * @param \Throwable $e
99642e976cSAndreas Gohr     * @return bool false if the logging failed
100642e976cSAndreas Gohr     */
101642e976cSAndreas Gohr    public static function logException($e)
102642e976cSAndreas Gohr    {
103*0ecde6ceSAndreas Gohr        return Logger::getInstance()->log(
104*0ecde6ceSAndreas Gohr            get_class($e) . ': ' . $e->getMessage(),
105*0ecde6ceSAndreas Gohr            $e->getTraceAsString(),
106*0ecde6ceSAndreas Gohr            $e->getFile(),
107*0ecde6ceSAndreas Gohr            $e->getLine()
108*0ecde6ceSAndreas Gohr        );
109642e976cSAndreas Gohr    }
1107e0b27f2SAndreas Gohr
1117e0b27f2SAndreas Gohr    /**
1127e0b27f2SAndreas Gohr     * Checks the the stacktrace for plugin files
1137e0b27f2SAndreas Gohr     *
1147e0b27f2SAndreas Gohr     * @param \Throwable $e
1157e0b27f2SAndreas Gohr     * @return false|string
1167e0b27f2SAndreas Gohr     */
1177e0b27f2SAndreas Gohr    protected static function guessPlugin($e)
1187e0b27f2SAndreas Gohr    {
119dbe0790dSAndreas Gohr        if (preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $e->getFile()), $match)) {
120dbe0790dSAndreas Gohr            return $match[1];
121dbe0790dSAndreas Gohr        }
122dbe0790dSAndreas Gohr
1237e0b27f2SAndreas Gohr        foreach ($e->getTrace() as $line) {
1247e0b27f2SAndreas Gohr            if (
1257e0b27f2SAndreas Gohr                isset($line['class']) &&
1267e0b27f2SAndreas Gohr                preg_match('/\w+?_plugin_(\w+)/', $line['class'], $match)
1277e0b27f2SAndreas Gohr            ) {
1287e0b27f2SAndreas Gohr                return $match[1];
1297e0b27f2SAndreas Gohr            }
1307e0b27f2SAndreas Gohr
1317e0b27f2SAndreas Gohr            if (
1327e0b27f2SAndreas Gohr                isset($line['file']) &&
1337e0b27f2SAndreas Gohr                preg_match('/lib\/plugins\/(\w+)\//', str_replace('\\', '/', $line['file']), $match)
1347e0b27f2SAndreas Gohr            ) {
1357e0b27f2SAndreas Gohr                return $match[1];
1367e0b27f2SAndreas Gohr            }
1377e0b27f2SAndreas Gohr        }
1387e0b27f2SAndreas Gohr
1397e0b27f2SAndreas Gohr        return false;
1407e0b27f2SAndreas Gohr    }
141642e976cSAndreas Gohr}
142