diff --git a/CHANGELOG.md b/CHANGELOG.md index 37dc4488..14d61d9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ Minimum PHP version for this release is 7.2.5. * support for Account Number * suggest account number option * many new functions, hooks and configurables +* better Exception Handler (Whoops - https://github.com/filp/whoops) ### Changed * Composer is now used for external libraries like: Twig, PHPMailer, fast-route etc. diff --git a/common.php b/common.php index 5adc7209..109bd3ae 100644 --- a/common.php +++ b/common.php @@ -141,8 +141,6 @@ if(!IS_CLI) { define('ADMIN_URL', SERVER_URL . BASE_DIR . '/' . ADMIN_PANEL_FOLDER . '/'); //define('CURRENT_URL', BASE_URL . $_SERVER['REQUEST_URI']); - - require SYSTEM . 'exception.php'; } $autoloadFile = VENDOR . 'autoload.php'; diff --git a/composer.json b/composer.json index cd16bf18..0a17947e 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ "twig/twig": "^2.0", "erusev/parsedown": "^1.7", "nikic/fast-route": "^1.3", - "matomo/device-detector": "^6.0" + "matomo/device-detector": "^6.0", + "filp/whoops": "^2.15" } } diff --git a/system/exception.php b/system/exception.php index e46f0d54..9a233000 100644 --- a/system/exception.php +++ b/system/exception.php @@ -1,54 +1,20 @@ * @copyright 2023 MyAAC * @link https://my-aac.org */ -require LIBS . 'SensitiveException.php'; -/** - * @param Exception $exception - */ -function exception_handler($exception) { - $message = $exception->getMessage(); - if($exception instanceof SensitiveException) { - $message = 'This error is sensitive and has been logged into ' . LOGS . 'error.log.
View this file for more information.'; +$whoops = new \Whoops\Run; - // log error to file - $f = fopen(LOGS . 'error.log', 'ab'); - if(!$f) { - $message = 'We wanted to save detailed informations about this error, but file: ' . LOGS . "error.log couldn't be opened for writing.. so the detailed information couldn't be saved.. are you sure directory system/logs is writable by web server? Correct this, and then refresh this site."; - } - else { - fwrite($f, '[' . date(DateTime::RFC1123) . '] ' . $exception->getMessage() . PHP_EOL); - fclose($f); - } - } - - $backtrace_formatted = nl2br($exception->getTraceAsString()); - - // display basic error message without template - // template is missing, why? probably someone deleted templates dir, or it wasn't downloaded right - $template_file = SYSTEM . 'templates/exception.html.twig'; - if(!file_exists($template_file)) { - echo 'Something went terribly wrong..

'; - echo "$message

"; - echo 'Backtrace:
'; - echo $backtrace_formatted; - return; - } - - // display beautiful error message - // the file is .twig.html, but its not really parsed by Twig - // we just replace some values manually - // cause in case Twig throws exception, we can show it too - $content = file_get_contents($template_file); - $content = str_replace(array('{{ BASE_URL }}', '{{ exceptionClass }}', '{{ message }}', '{{ backtrace }}', '{{ powered_by }}'), array(BASE_URL, get_class($exception), $message, $backtrace_formatted, base64_decode('UG93ZXJlZCBieSA8YSBocmVmPSJodHRwOi8vbXktYWFjLm9yZyIgdGFyZ2V0PSJfYmxhbmsiPk15QUFDLjwvYT4=')), $content); - - echo $content; +if(IS_CLI) { + $whoops->pushHandler(new \Whoops\Handler\PlainTextHandler); +} +else { + $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler); } -set_exception_handler('exception_handler'); +$whoops->register(); diff --git a/system/init.php b/system/init.php index f2ea9ee2..056bbd40 100644 --- a/system/init.php +++ b/system/init.php @@ -18,6 +18,10 @@ if(!isset($config['installed']) || !$config['installed']) { throw new RuntimeException('MyAAC has not been installed yet or there was error during installation. Please install again.'); } +if(config('env') === 'dev') { + require SYSTEM . 'exception.php'; +} + date_default_timezone_set($config['date_timezone']); // take care of trailing slash at the end if($config['server_path'][strlen($config['server_path']) - 1] !== '/') diff --git a/system/libs/SensitiveException.php b/system/libs/SensitiveException.php deleted file mode 100644 index 58978198..00000000 --- a/system/libs/SensitiveException.php +++ /dev/null @@ -1,3 +0,0 @@ - - - - - - - Something went wrong... - - - - - - - - - - - -
-

Whoops something went wrong...

-
- Exception class: {{ exceptionClass }}() -

- {{ message }} -


- Backtrace:

- {{ backtrace }} -
-
- - -