Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Error | |
100.00% |
26 / 26 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
| errorHandler | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
6 | |||
| exceptionHandler | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * phpMyFAQ main error class. |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | * @copyright 2020-2026 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2020-11-13 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Core; |
| 21 | |
| 22 | use ErrorException; |
| 23 | use phpMyFAQ\Environment; |
| 24 | use phpMyFAQ\Strings; |
| 25 | |
| 26 | /** |
| 27 | * Class Error |
| 28 | * |
| 29 | * @package phpMyFAQ |
| 30 | */ |
| 31 | class Error |
| 32 | { |
| 33 | /** |
| 34 | * Error handler to convert all errors to PHP exceptions by |
| 35 | * throwing a PHP ErrorException. Deprecation notices are only |
| 36 | * logged, so they never crash the application. |
| 37 | * |
| 38 | * @throws ErrorException |
| 39 | */ |
| 40 | public static function errorHandler(int $level, string $message, string $filename, int $line): void |
| 41 | { |
| 42 | if (error_reporting() === 0) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | if ($level === E_DEPRECATED || $level === E_USER_DEPRECATED) { |
| 47 | if (ini_get('log_errors')) { |
| 48 | error_log(sprintf('phpMyFAQ Deprecation: %s in %s on line %d', $message, $filename, $line)); |
| 49 | } |
| 50 | |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | $filename = Environment::isDebugMode() ? $filename : basename($filename); |
| 55 | throw new ErrorException($message, 0, $level, $filename, $line); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Exception handler. |
| 60 | */ |
| 61 | public static function exceptionHandler(\Throwable $exception): void |
| 62 | { |
| 63 | $code = $exception->getCode(); |
| 64 | if ($code !== 404) { |
| 65 | $code = 500; |
| 66 | } |
| 67 | |
| 68 | http_response_code($code); |
| 69 | |
| 70 | echo '<h1>phpMyFAQ Fatal error</h1>'; |
| 71 | echo "<p>Uncaught exception: '" . $exception::class . "'</p>"; |
| 72 | echo "<p>Message: '" . Strings::htmlentities($exception->getMessage()) . "'</p>"; |
| 73 | echo '<p>Stack trace:<pre>' . $exception->getTraceAsString() . '</pre></p>'; |
| 74 | echo "<p>Thrown in '" . $exception->getFile() . "' on line " . $exception->getLine() . '</p>'; |
| 75 | if (ini_get('log_errors')) { |
| 76 | error_log(sprintf( |
| 77 | "phpMyFAQ %s: %s in %s on line %d\nStack trace:\n%s", |
| 78 | $exception::class, |
| 79 | $exception->getMessage(), |
| 80 | $exception->getFile(), |
| 81 | $exception->getLine(), |
| 82 | $exception->getTraceAsString(), |
| 83 | )); |
| 84 | } |
| 85 | } |
| 86 | } |