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.
 
 
 
 
 
 

137 lines
3.8 KiB

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use think\console\Output as ConsoleOutput;
  13. use think\exception\ErrorException;
  14. use think\exception\Handle;
  15. use think\exception\ThrowableError;
  16. class Error
  17. {
  18. /**
  19. * 注册异常处理
  20. * @access public
  21. * @return void
  22. */
  23. public static function register()
  24. {
  25. error_reporting(E_ALL);
  26. set_error_handler([__CLASS__, 'appError']);
  27. set_exception_handler([__CLASS__, 'appException']);
  28. register_shutdown_function([__CLASS__, 'appShutdown']);
  29. }
  30. /**
  31. * 异常处理
  32. * @access public
  33. * @param \Exception|\Throwable $e 异常
  34. * @return void
  35. */
  36. public static function appException($e)
  37. {
  38. if (!$e instanceof \Exception) {
  39. $e = new ThrowableError($e);
  40. }
  41. $handler = self::getExceptionHandler();
  42. $handler->report($e);
  43. if (IS_CLI) {
  44. $handler->renderForConsole(new ConsoleOutput, $e);
  45. } else {
  46. $handler->render($e)->send();
  47. }
  48. }
  49. /**
  50. * 错误处理
  51. * @access public
  52. * @param integer $errno 错误编号
  53. * @param integer $errstr 详细错误信息
  54. * @param string $errfile 出错的文件
  55. * @param integer $errline 出错行号
  56. * @return void
  57. * @throws ErrorException
  58. */
  59. public static function appError($errno, $errstr, $errfile = '', $errline = 0)
  60. {
  61. $exception = new ErrorException($errno, $errstr, $errfile, $errline);
  62. // 符合异常处理的则将错误信息托管至 think\exception\ErrorException
  63. if (error_reporting() & $errno) {
  64. throw $exception;
  65. }
  66. self::getExceptionHandler()->report($exception);
  67. }
  68. /**
  69. * 异常中止处理
  70. * @access public
  71. * @return void
  72. */
  73. public static function appShutdown()
  74. {
  75. // 将错误信息托管至 think\ErrorException
  76. if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {
  77. self::appException(new ErrorException(
  78. $error['type'], $error['message'], $error['file'], $error['line']
  79. ));
  80. }
  81. // 写入日志
  82. Log::save();
  83. }
  84. /**
  85. * 确定错误类型是否致命
  86. * @access protected
  87. * @param int $type 错误类型
  88. * @return bool
  89. */
  90. protected static function isFatal($type)
  91. {
  92. return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
  93. }
  94. /**
  95. * 获取异常处理的实例
  96. * @access public
  97. * @return Handle
  98. */
  99. public static function getExceptionHandler()
  100. {
  101. static $handle;
  102. if (!$handle) {
  103. // 异常处理 handle
  104. $class = Config::get('exception_handle');
  105. if ($class && is_string($class) && class_exists($class) &&
  106. is_subclass_of($class, "\\think\\exception\\Handle")
  107. ) {
  108. $handle = new $class;
  109. } else {
  110. $handle = new Handle;
  111. if ($class instanceof \Closure) {
  112. $handle->setRender($class);
  113. }
  114. }
  115. }
  116. return $handle;
  117. }
  118. }