暫無描述

ErrorException.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\Exception;
  11. /**
  12. * A custom error Exception for Psy with a formatted $message.
  13. */
  14. class ErrorException extends \ErrorException implements Exception
  15. {
  16. private $rawMessage;
  17. /**
  18. * Construct a Psy ErrorException.
  19. *
  20. * @param string $message (default: "")
  21. * @param int $code (default: 0)
  22. * @param int $severity (default: 1)
  23. * @param string $filename (default: null)
  24. * @param int $lineno (default: null)
  25. * @param Exception $previous (default: null)
  26. */
  27. public function __construct($message = '', $code = 0, $severity = 1, $filename = null, $lineno = null, $previous = null)
  28. {
  29. $this->rawMessage = $message;
  30. if (!empty($filename) && \preg_match('{Psy[/\\\\]ExecutionLoop}', $filename)) {
  31. $filename = '';
  32. }
  33. switch ($severity) {
  34. case E_STRICT:
  35. $type = 'Strict error';
  36. break;
  37. case E_NOTICE:
  38. case E_USER_NOTICE:
  39. $type = 'Notice';
  40. break;
  41. case E_WARNING:
  42. case E_CORE_WARNING:
  43. case E_COMPILE_WARNING:
  44. case E_USER_WARNING:
  45. $type = 'Warning';
  46. break;
  47. case E_DEPRECATED:
  48. case E_USER_DEPRECATED:
  49. $type = 'Deprecated';
  50. break;
  51. case E_RECOVERABLE_ERROR:
  52. $type = 'Recoverable fatal error';
  53. break;
  54. default:
  55. $type = 'Error';
  56. break;
  57. }
  58. $message = \sprintf('PHP %s: %s%s on line %d', $type, $message, $filename ? ' in ' . $filename : '', $lineno);
  59. parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
  60. }
  61. /**
  62. * Get the raw (unformatted) message for this error.
  63. *
  64. * @return string
  65. */
  66. public function getRawMessage()
  67. {
  68. return $this->rawMessage;
  69. }
  70. /**
  71. * Helper for throwing an ErrorException.
  72. *
  73. * This allows us to:
  74. *
  75. * set_error_handler(array('Psy\Exception\ErrorException', 'throwException'));
  76. *
  77. * @throws ErrorException
  78. *
  79. * @param int $errno Error type
  80. * @param string $errstr Message
  81. * @param string $errfile Filename
  82. * @param int $errline Line number
  83. */
  84. public static function throwException($errno, $errstr, $errfile, $errline)
  85. {
  86. throw new self($errstr, 0, $errno, $errfile, $errline);
  87. }
  88. /**
  89. * Create an ErrorException from an Error.
  90. *
  91. * @param \Error $e
  92. *
  93. * @return ErrorException
  94. */
  95. public static function fromError(\Error $e)
  96. {
  97. return new self($e->getMessage(), $e->getCode(), 1, $e->getFile(), $e->getLine(), $e);
  98. }
  99. }