/**
  * @param \Throwable $exception
  *
  * @return bool
  */
 public function exceptionHandler(\Throwable $exception)
 {
     if ($exception instanceof ResponseCode) {
         try {
             $out = $this->render($exception->getCode(), [], $exception->display());
         } catch (\Throwable $exception) {
             return $this->exceptionHandler($exception);
         }
         // debug on dev / display trace
         if (!(AbstractApp::env() != AbstractApp::PRODUCTION && ob_get_length() > 0)) {
             self::response()->addHeader('Content-Type', $this->getErrorContentType());
         }
         self::response()->setStatus($exception->getCode());
         self::response()->setBody($out);
         HttpApp::instance()->end();
     } else {
         Handler::log($exception);
         if (AbstractApp::env() != AbstractApp::PRODUCTION) {
             Handler::exceptionHandler($exception);
         } else {
             $throw = new ResponseCode($exception->getMessage(), 500, $exception);
             $this->exceptionHandler($throw);
         }
     }
     return true;
 }
Esempio n. 2
0
 /**
  * @param \Throwable $exception
  *
  * @return bool
  */
 public static function exceptionHandler(\Throwable $exception)
 {
     // This error code is not included in error_reporting
     if (!error_reporting() || $exception->getLine() == 0) {
         return;
     }
     if ('cli' === PHP_SAPI || isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'curl') !== false) {
         $formatterClass = '\\Cawa\\Error\\Formatter\\CliFormatter';
     } else {
         $formatterClass = '\\Cawa\\Error\\Formatter\\HtmlFormatter';
     }
     if (AbstractApp::isInit() && AbstractApp::instance() instanceof HttpApp) {
         self::log($exception);
         self::response()->setStatus(500);
         if (AbstractApp::env() != AbstractApp::PROD || Ip::isAdmin()) {
             $formatter = new $formatterClass();
             self::render($formatter, $exception);
         } else {
             self::clearAllBuffer();
             echo self::router()->returnError(500);
         }
         AbstractApp::instance()->end();
     } else {
         if (!headers_sent()) {
             header('HTTP/1.1 500 Internal Server Error');
         }
         if (AbstractApp::env() != AbstractApp::PROD) {
             $formatter = new $formatterClass();
             self::render($formatter, $exception);
         } else {
             self::clearAllBuffer();
             echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">' . "\n" . '<html><head>' . "\n" . '<title>' . self::response()->getStatusString(500) . '</title>' . "\n" . '</head><body>' . "\n" . '<h1>' . self::response()->getStatusString(500) . '</h1>' . "\n" . '</body></html>';
         }
     }
 }