GBException::format PHP Method

format() public static method

Render a full HTML description of an exception
See also: formatTrace()
public static format ( Exception $e, $includingTrace = true, $html = null, $skip = null, $context_lines = 2 ) : string
$e Exception
return string
    public static function format(Exception $e, $includingTrace = true, $html = null, $skip = null, $context_lines = 2)
    {
        if ($html === null) {
            $html = ini_get('html_errors') ? true : false;
        }
        $tracestr = $includingTrace ? self::formatTrace($e, $html, $skip, $context_lines) : false;
        $code = $e->getCode();
        $message = $e instanceof self ? $e->formatMessage($html) : ($html ? nl2br(h($e->getMessage())) : $e->getMessage());
        try {
            $context = '';
            if ($context_lines && $e->getFile() && $e->getLine()) {
                $context = self::formatSourceLines($e->getFile(), $e->getLine(), $html, $context_lines);
                if ($context) {
                    $context = $html ? '<pre class="context">' . $context . '</pre>' : "\n\n" . $context . "\n\n";
                } else {
                    $context = '';
                }
            }
            if ($html) {
                $str = '<div class="exception"><h2>' . get_class($e);
                if ($code) {
                    $str .= ' <span class="code">(' . $code . ')</span>';
                }
                $abs = $e->getFile();
                $rel = gb_relpath(gb::$site_dir, $abs);
                $pathprefix = substr($abs, 0, -strlen($rel));
                $str .= '</h2>' . '<p class="message">' . $message . '</p> ' . '<p class="location">' . 'in <span class="location"><span class="prefix">' . $pathprefix . '</span>' . $rel . ':' . $e->getLine() . '</span>' . '</p>' . $context;
            } else {
                $str = get_class($e) . ($code ? ' (' . $code . '): ' : ': ') . $message . ' on line ' . $e->getLine() . ' in ' . $e->getFile() . $context;
            }
            if ($includingTrace) {
                $str .= "\n" . $tracestr;
            }
            # caused by...
            $previous = self::getPreviousCompat($e);
            if ($previous && $previous instanceof Exception) {
                # never include trace from caused php exception, because it is the same as it's parent.
                $inc_prev_trace = !$previous instanceof PHPException;
                $prev_fmt = self::format($previous, $inc_prev_trace, $html, $skip, $context_lines);
                if ($html) {
                    $str .= '<b>Caused by:</b><div style="margin-left:15px">' . $prev_fmt . '</div>';
                } else {
                    $str .= "\nCaused by:\n  " . str_replace("\n", "\n  ", $prev_fmt) . "\n";
                }
            }
            if ($html) {
                $str .= '</div>';
            }
            return $str;
        } catch (Exception $ex) {
            $str = get_class($e) . ': ' . $message;
            if ($includingTrace) {
                $str .= "\n" . $tracestr;
            }
            return $html ? nl2br(h($str)) : $str;
        }
    }

Usage Example

コード例 #1
0
ファイル: GBRebuilder.php プロジェクト: rsms/gitblog
 /**
  * Rebuild caches, indexes, etc.
  */
 static function rebuild($forceFullRebuild = false)
 {
     gb::log(LOG_NOTICE, 'rebuilding cache' . ($forceFullRebuild ? ' (forcing full rebuild)' : ''));
     $time_started = microtime(1);
     $failures = array();
     # Load rebuild plugins
     gb::load_plugins('rebuild');
     # Load rebuilders if needed
     if (empty(self::$rebuilders)) {
         self::loadRebuilders();
     }
     # Create rebuilder instances
     $rebuilders = array();
     foreach (self::$rebuilders as $cls) {
         $rebuilders[] = new $cls($forceFullRebuild);
     }
     # Load rebuild plugins (2nd offer)
     gb::load_plugins('rebuild');
     # Query ls-tree
     $ls = rtrim(git::exec('ls-files --stage'));
     if ($ls) {
         # Iterate objects
         $ls = explode("\n", $ls);
         foreach ($ls as $line) {
             try {
                 # <mode> SP <object> SP <stage no> TAB <name>
                 if (!$line) {
                     continue;
                 }
                 $line = explode(' ', $line, 3);
                 $id = $line[1];
                 $name = gb_normalize_git_name(substr($line[2], strpos($line[2], "\t") + 1));
                 foreach ($rebuilders as $rebuilder) {
                     $rebuilder->onObject($name, $id);
                 }
             } catch (RuntimeException $e) {
                 gb::log(LOG_ERR, 'failed to rebuild object %s %s: %s', var_export($name, 1), $e->getMessage(), $e->getTraceAsString());
                 $failures[] = array($rebuilder, $name);
             }
         }
     }
     # Let rebuilders finalize
     foreach ($rebuilders as $rebuilder) {
         try {
             $rebuilder->finalize();
         } catch (RuntimeException $e) {
             gb::log(LOG_ERR, 'rebuilder %s (0x%x) failed to finalize: %s', get_class($rebuilder), spl_object_hash($rebuilder), GBException::format($e, true, false, null, 0));
             $failures[] = array($rebuilder, null);
         }
     }
     gb::log(LOG_NOTICE, 'cache updated -- time spent: %s', gb_format_duration(microtime(1) - $time_started));
     return $failures;
 }
All Usage Examples Of GBException::format