Cml\Console\IO\Output::writeException PHP Method

writeException() public static method

输出异常错误信息
public static writeException ( mixed $e )
$e mixed
    public static function writeException($e)
    {
        if ($e instanceof \Exception) {
            $text = sprintf("%s\n[%s]\n%s", $e->getFile() . ':' . $e->getLine(), get_class($e), $e->getMessage());
        } else {
            $text = $e;
        }
        $box = new Box($text, '*');
        $out = Colour::colour($box, [Colour::WHITE, 0], Colour::RED);
        $format = new Format(['indent' => 2]);
        $out = $format->format($out);
        self::writeln($out, STDERR);
    }

Usage Example

Esempio n. 1
0
 /**
  * 自定义异常处理
  *
  * @param mixed $e 异常对象
  */
 public function appException(&$e)
 {
     $error = [];
     $exceptionClass = new \ReflectionClass($e);
     $error['exception'] = '\\' . $exceptionClass->name;
     $error['message'] = $e->getMessage();
     $trace = $e->getTrace();
     foreach ($trace as $key => $val) {
         $error['files'][$key] = $val;
     }
     if (substr($e->getFile(), -20) !== '\\Tools\\functions.php' || $e->getLine() !== 90) {
         array_unshift($error['files'], ['file' => $e->getFile(), 'line' => $e->getLine(), 'type' => 'throw']);
     }
     if (!Cml::$debug) {
         //正式环境 只显示‘系统错误’并将错误信息记录到日志
         Log::emergency($error['message'], [$error['files'][0]]);
         $error = [];
         $error['message'] = Lang::get('_CML_ERROR_');
     }
     if (Request::isCli()) {
         Output::writeException(sprintf("%s\n[%s]\n%s", isset($error['files']) ? implode($error['files'][0], ':') : '', get_class($e), $error['message']));
     } else {
         header('HTTP/1.1 500 Internal Server Error');
         View::getEngine('html')->reset()->assign('error', $error);
         Cml::showSystemTemplate(Config::get('html_exception'));
     }
 }
All Usage Examples Of Cml\Console\IO\Output::writeException