ErrorHandler.php 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 UnknownCommandException) {
  30. // display message and suggest alternatives in case of unknown command
  31. $message = $this->formatMessage($exception->getName() . ': ') . $exception->command;
  32. $alternatives = $exception->getSuggestedAlternatives();
  33. if (count($alternatives) === 1) {
  34. $message .= "\n\nDid you mean \"" . reset($alternatives) . '"?';
  35. } elseif (count($alternatives) > 1) {
  36. $message .= "\n\nDid you mean one of these?\n - " . implode("\n - ", $alternatives);
  37. }
  38. } elseif ($exception instanceof Exception && ($exception instanceof UserException || !YII_DEBUG)) {
  39. $message = $this->formatMessage($exception->getName() . ': ') . $exception->getMessage();
  40. } elseif (YII_DEBUG) {
  41. if ($exception instanceof Exception) {
  42. $message = $this->formatMessage("Exception ({$exception->getName()})");
  43. } elseif ($exception instanceof ErrorException) {
  44. $message = $this->formatMessage($exception->getName());
  45. } else {
  46. $message = $this->formatMessage('Exception');
  47. }
  48. $message .= $this->formatMessage(" '" . get_class($exception) . "'", [Console::BOLD, Console::FG_BLUE])
  49. . ' with message ' . $this->formatMessage("'{$exception->getMessage()}'", [Console::BOLD]) //. "\n"
  50. . "\n\nin " . dirname($exception->getFile()) . DIRECTORY_SEPARATOR . $this->formatMessage(basename($exception->getFile()), [Console::BOLD])
  51. . ':' . $this->formatMessage($exception->getLine(), [Console::BOLD, Console::FG_YELLOW]) . "\n";
  52. if ($exception instanceof \yii\db\Exception && !empty($exception->errorInfo)) {
  53. $message .= "\n" . $this->formatMessage("Error Info:\n", [Console::BOLD]) . print_r($exception->errorInfo, true);
  54. }
  55. $message .= "\n" . $this->formatMessage("Stack trace:\n", [Console::BOLD]) . $exception->getTraceAsString();
  56. } else {
  57. $message = $this->formatMessage('Error: ') . $exception->getMessage();
  58. }
  59. if (PHP_SAPI === 'cli') {
  60. Console::stderr($message . "\n");
  61. } else {
  62. echo $message . "\n";
  63. }
  64. }
  65. /**
  66. * Colorizes a message for console output.
  67. * @param string $message the message to colorize.
  68. * @param array $format the message format.
  69. * @return string the colorized message.
  70. * @see Console::ansiFormat() for details on how to specify the message format.
  71. */
  72. protected function formatMessage($message, $format = [Console::FG_RED, Console::BOLD])
  73. {
  74. $stream = (PHP_SAPI === 'cli') ? \STDERR : \STDOUT;
  75. // try controller first to allow check for --color switch
  76. if (Yii::$app->controller instanceof \yii\console\Controller && Yii::$app->controller->isColorEnabled($stream)
  77. || Yii::$app instanceof \yii\console\Application && Console::streamSupportsAnsiColors($stream)) {
  78. $message = Console::ansiFormat($message, $format);
  79. }
  80. return $message;
  81. }
  82. }