/** * Converts an exception into a simple string. * @param \Exception $exception the exception being converted * @return string the string representation of the exception. */ public static function convertExceptionToString($exception) { if ($exception instanceof Exception && ($exception instanceof UserException || !YII_DEBUG)) { $message = "{$exception->getName()}: {$exception->getMessage()}"; } elseif (YII_DEBUG) { if ($exception instanceof Exception) { $message = "Exception ({$exception->getName()})"; } elseif ($exception instanceof ErrorException) { $message = "{$exception->getName()}"; } else { $message = 'Exception'; } $message .= " '" . get_class($exception) . "' with message '{$exception->getMessage()}' \n\nin " . $exception->getFile() . ':' . $exception->getLine() . "\n\n" . "Stack trace:\n" . $exception->getTraceAsString(); } else { $message = 'Error: ' . $exception->getMessage(); } return $message; }
/** * Renders an exception without using rich format. * @param \Exception $exception the exception to be rendered. * @return string the rendering result */ public function renderException($exception) { if ($exception instanceof Exception && ($exception instanceof UserException || !YII_DEBUG)) { $message = $exception->getName() . ': ' . $exception->getMessage(); if (Yii::$app->controller instanceof \yii\console\Controller) { $message = Yii::$app->controller->ansiFormat($message, Console::FG_RED); } } else { $message = YII_DEBUG ? (string) $exception : 'Error: ' . $exception->getMessage(); } if (PHP_SAPI === 'cli') { return $message . "\n"; } else { return '<pre>' . htmlspecialchars($message, ENT_QUOTES, $this->charset) . '</pre>'; } }