ErrorHandler.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. /**
  8. * An error handler that converts runtime errors into exceptions
  9. */
  10. class ErrorHandler
  11. {
  12. /**
  13. * Error messages
  14. *
  15. * @var array
  16. */
  17. protected $errorPhrases = [
  18. E_ERROR => 'Error',
  19. E_WARNING => 'Warning',
  20. E_PARSE => 'Parse Error',
  21. E_NOTICE => 'Notice',
  22. E_CORE_ERROR => 'Core Error',
  23. E_CORE_WARNING => 'Core Warning',
  24. E_COMPILE_ERROR => 'Compile Error',
  25. E_COMPILE_WARNING => 'Compile Warning',
  26. E_USER_ERROR => 'User Error',
  27. E_USER_WARNING => 'User Warning',
  28. E_USER_NOTICE => 'User Notice',
  29. E_STRICT => 'Strict Notice',
  30. E_RECOVERABLE_ERROR => 'Recoverable Error',
  31. E_DEPRECATED => 'Deprecated Functionality',
  32. E_USER_DEPRECATED => 'User Deprecated Functionality',
  33. ];
  34. /**
  35. * Custom error handler
  36. *
  37. * @param int $errorNo
  38. * @param string $errorStr
  39. * @param string $errorFile
  40. * @param int $errorLine
  41. * @return bool
  42. * @throws \Exception
  43. */
  44. public function handler($errorNo, $errorStr, $errorFile, $errorLine)
  45. {
  46. if (strpos($errorStr, 'DateTimeZone::__construct') !== false) {
  47. // there's no way to distinguish between caught system exceptions and warnings
  48. return false;
  49. }
  50. $errorNo = $errorNo & error_reporting();
  51. if ($errorNo == 0) {
  52. return false;
  53. }
  54. $msg = isset($this->errorPhrases[$errorNo]) ? $this->errorPhrases[$errorNo] : "Unknown error ({$errorNo})";
  55. $msg .= ": {$errorStr} in {$errorFile} on line {$errorLine}";
  56. throw new \Exception($msg);
  57. }
  58. }