Exemplo n.º 1
0
 /**
  * Handler to catch uncaught exception.
  * @param  \Exception|\Throwable
  * @return void
  * @internal
  */
 public static function exceptionHandler($exception, $exit = TRUE)
 {
     if (!self::$reserved) {
         return;
     }
     self::$reserved = NULL;
     if (!headers_sent()) {
         $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
         $code = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE ? 503 : 500;
         header("{$protocol} {$code}", TRUE, $code);
         if (self::isHtmlMode()) {
             header('Content-Type: text/html; charset=UTF-8');
         }
     }
     Helpers::improveException($exception);
     if (self::$productionMode) {
         try {
             self::log($exception, self::EXCEPTION);
         } catch (\Throwable $e) {
         } catch (\Exception $e) {
         }
         if (self::isHtmlMode()) {
             $logged = empty($e);
             require self::$errorTemplate ?: __DIR__ . '/assets/Debugger/error.500.phtml';
         } elseif (PHP_SAPI === 'cli') {
             fwrite(STDERR, 'ERROR: application encountered an error and can not continue. ' . (isset($e) ? "Unable to log error.\n" : "Error was logged.\n"));
         }
     } elseif (!connection_aborted() && self::isHtmlMode()) {
         self::getBlueScreen()->render($exception);
         if (self::$showBar) {
             self::getBar()->render();
         }
     } else {
         self::fireLog($exception);
         $s = get_class($exception) . ($exception->getMessage() === '' ? '' : ': ' . $exception->getMessage()) . ' in ' . $exception->getFile() . ':' . $exception->getLine() . "\nStack trace:\n" . $exception->getTraceAsString();
         try {
             $file = self::log($exception, self::EXCEPTION);
             if ($file && !headers_sent()) {
                 header("X-Tracy-Error-Log: {$file}");
             }
             echo "{$s}\n" . ($file ? "(stored in {$file})\n" : '');
             if ($file && self::$browser) {
                 exec(self::$browser . ' ' . escapeshellarg($file));
             }
         } catch (\Throwable $e) {
             echo "{$s}\nUnable to log error: {$e->getMessage()}\n";
         } catch (\Exception $e) {
             echo "{$s}\nUnable to log error: {$e->getMessage()}\n";
         }
     }
     try {
         $e = NULL;
         foreach (self::$onFatalError as $handler) {
             call_user_func($handler, $exception);
         }
     } catch (\Throwable $e) {
     } catch (\Exception $e) {
     }
     if ($e) {
         try {
             self::log($e, self::EXCEPTION);
         } catch (\Throwable $e) {
         } catch (\Exception $e) {
         }
     }
     if ($exit) {
         exit($exception instanceof \Error ? 255 : 254);
     }
 }