Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Configure error reporting
- */
- if (is_webserver()) {
- /* running under a web server */
- ini_set('html_errors', '1');
- ini_set('display_errors', defined('APPLICATION_DEVENV') ? '1' : '0');
- } else {
- /* Command Line Interface (CLI) */
- ini_set('html_errors', '0');
- ini_set('display_errors', 'stderr');
- }
- ini_set('log_errors', '0');
- ini_set('error_log', <path to directory> . '\error.log'));
- /* define the default exception handler */
- set_exception_handler('exception_handler');
- /* convert errors to exceptions (fatal errors are not handled by a user-defined error handler) */
- set_error_handler(function (int $err_type, string $err_message, string $err_file = '', int $err_line = 0) {
- throw new ErrorException($err_message, 0, $err_type, $err_file, $err_line);
- });
- /* handle fatal errors */
- register_shutdown_function(function () {
- /* only unhandled errors (ie fatal errors) should have the last error set */
- if (! is_null($err = error_get_last())) {
- if (in_array($err['type'], [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR])) {
- /* execution halted - record the error information on the error log */
- error_log(sprintf('Fatal error: %s in %s: %d',
- $err['message'], $err['file'], $err['line']));
- } else {
- /* convert fatal errors to a CALL to the default exception handler */
- $e = new ErrorException($err['message'], 0, $err['type'], $err['file'], $err['line']);
- exception_handler($e);
- }
- }
- });
- /**
- * Default exception handler if an exception is not caught in a try/catch block.
- *
- * @param \Throwable $t Exception/Error
- *
- * @return void
- */
- function exception_handler(\Throwable $t): void
- {
- ... filter out missing web assets, renew session, determine http code, etc ...
- /* record the exception on the error log */
- error_log(sprintf('%s: %s in %s: %d',
- get_class($t), $t->getMessage(), $t->getFile(), $t->getLine()));
- /*
- * Display the exception
- */
- if (filter_var(ini_get('html_errors'), FILTER_VALIDATE_BOOL)) {
- http_response_code($http_code);
- if (filter_var(ini_get('display_errors'), FILTER_VALIDATE_BOOL)) {
- /* generate stack trace (development environment) */
- exception_stacktrace($t);
- } else {
- /* generate user friendly exception page */
- exception_infopage($t);
- }
- } else {
- echo PHP_EOL, $t->getMessage(), PHP_EOL;
- }
- }
- /**
- * Determine whether running under a web server.
- *
- * @return bool Indicates whether running under a web server
- */
- function is_webserver(): bool
- {
- return ! (defined('STDIN') || ! empty($_SERVER['argc']));
- }
Advertisement
Add Comment
Please, Sign In to add comment