You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ErrorHandler.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\console;
  8. use Yii;
  9. use yii\base\ErrorException;
  10. use yii\base\UserException;
  11. use yii\helpers\Console;
  12. /**
  13. * ErrorHandler handles uncaught PHP errors and exceptions.
  14. *
  15. * ErrorHandler is configured as an application component in [[\yii\base\Application]] by default.
  16. * You can access that instance via `Yii::$app->errorHandler`.
  17. *
  18. * @author Carsten Brandt <mail@cebe.cc>
  19. * @since 2.0
  20. */
  21. class ErrorHandler extends \yii\base\ErrorHandler
  22. {
  23. /**
  24. * Renders an exception using ansi format for console output.
  25. * @param \Exception $exception the exception to be rendered.
  26. */
  27. protected function renderException($exception)
  28. {
  29. if ($exception instanceof Exception && ($exception instanceof UserException || !YII_DEBUG)) {
  30. $message = $this->formatMessage($exception->getName() . ': ') . $exception->getMessage();
  31. } elseif (YII_DEBUG) {
  32. if ($exception instanceof Exception) {
  33. $message = $this->formatMessage("Exception ({$exception->getName()})");
  34. } elseif ($exception instanceof ErrorException) {
  35. $message = $this->formatMessage($exception->getName());
  36. } else {
  37. $message = $this->formatMessage('Exception');
  38. }
  39. $message .= $this->formatMessage(" '" . get_class($exception) . "'", [Console::BOLD, Console::FG_BLUE])
  40. . ' with message ' . $this->formatMessage("'{$exception->getMessage()}'", [Console::BOLD]) //. "\n"
  41. . "\n\nin " . dirname($exception->getFile()) . DIRECTORY_SEPARATOR . $this->formatMessage(basename($exception->getFile()), [Console::BOLD])
  42. . ':' . $this->formatMessage($exception->getLine(), [Console::BOLD, Console::FG_YELLOW]) . "\n";
  43. if ($exception instanceof \yii\db\Exception && !empty($exception->errorInfo)) {
  44. $message .= "\n" . $this->formatMessage("Error Info:\n", [Console::BOLD]) . print_r($exception->errorInfo, true);
  45. }
  46. $message .= "\n" . $this->formatMessage("Stack trace:\n", [Console::BOLD]) . $exception->getTraceAsString();
  47. } else {
  48. $message = $this->formatMessage('Error: ') . $exception->getMessage();
  49. }
  50. if (PHP_SAPI === 'cli') {
  51. Console::stderr($message . "\n");
  52. } else {
  53. echo $message . "\n";
  54. }
  55. }
  56. /**
  57. * Colorizes a message for console output.
  58. * @param string $message the message to colorize.
  59. * @param array $format the message format.
  60. * @return string the colorized message.
  61. * @see Console::ansiFormat() for details on how to specify the message format.
  62. */
  63. protected function formatMessage($message, $format = [Console::FG_RED, Console::BOLD])
  64. {
  65. $stream = (PHP_SAPI === 'cli') ? \STDERR : \STDOUT;
  66. // try controller first to allow check for --color switch
  67. if (Yii::$app->controller instanceof \yii\console\Controller && Yii::$app->controller->isColorEnabled($stream)
  68. || Yii::$app instanceof \yii\console\Application && Console::streamSupportsAnsiColors($stream)) {
  69. $message = Console::ansiFormat($message, $format);
  70. }
  71. return $message;
  72. }
  73. }