Error.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 app\main\service\LogService;
  13. use think\console\Output as ConsoleOutput;
  14. use think\exception\ErrorException;
  15. use think\exception\Handle;
  16. use think\exception\HttpException;
  17. use think\exception\ThrowableError;
  18. class Error
  19. {
  20. /**
  21. * 注册异常处理
  22. * @access public
  23. * @return void
  24. */
  25. public static function register()
  26. {
  27. error_reporting(E_ALL);
  28. set_error_handler([__CLASS__, 'appError']);
  29. set_exception_handler([__CLASS__, 'appException']);
  30. register_shutdown_function([__CLASS__, 'appShutdown']);
  31. }
  32. /**
  33. * 异常处理
  34. * @access public
  35. * @param \Exception|\Throwable $e 异常
  36. * @return void
  37. */
  38. public static function appException($e)
  39. {
  40. if (!$e instanceof \Exception) {
  41. $e = new ThrowableError($e);
  42. }
  43. if (!($e instanceof HttpException)) {
  44. LogService::exception($e);
  45. }
  46. $handler = self::getExceptionHandler();
  47. $handler->report($e);
  48. if (IS_CLI) {
  49. $handler->renderForConsole(new ConsoleOutput, $e);
  50. } else {
  51. $handler->render($e)->send();
  52. }
  53. }
  54. /**
  55. * 错误处理
  56. * @access public
  57. * @param integer $errno 错误编号
  58. * @param integer $errstr 详细错误信息
  59. * @param string $errfile 出错的文件
  60. * @param integer $errline 出错行号
  61. * @return void
  62. * @throws ErrorException
  63. */
  64. public static function appError($errno, $errstr, $errfile = '', $errline = 0)
  65. {
  66. $exception = new ErrorException($errno, $errstr, $errfile, $errline);
  67. // 符合异常处理的则将错误信息托管至 think\exception\ErrorException
  68. if (error_reporting() & $errno) {
  69. throw $exception;
  70. }
  71. self::getExceptionHandler()->report($exception);
  72. }
  73. /**
  74. * 异常中止处理
  75. * @access public
  76. * @return void
  77. */
  78. public static function appShutdown()
  79. {
  80. // 将错误信息托管至 think\ErrorException
  81. if (!is_null($error = error_get_last()) && self::isFatal($error['type'])) {
  82. self::appException(new ErrorException(
  83. $error['type'], $error['message'], $error['file'], $error['line']
  84. ));
  85. }
  86. // 写入日志
  87. Log::save();
  88. }
  89. /**
  90. * 确定错误类型是否致命
  91. * @access protected
  92. * @param int $type 错误类型
  93. * @return bool
  94. */
  95. protected static function isFatal($type)
  96. {
  97. return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);
  98. }
  99. /**
  100. * 获取异常处理的实例
  101. * @access public
  102. * @return Handle
  103. */
  104. public static function getExceptionHandler()
  105. {
  106. static $handle;
  107. if (!$handle) {
  108. // 异常处理 handle
  109. $class = Config::get('exception_handle');
  110. if ($class && is_string($class) && class_exists($class) &&
  111. is_subclass_of($class, "\\think\\exception\\Handle")
  112. ) {
  113. $handle = new $class;
  114. } else {
  115. $handle = new Handle;
  116. if ($class instanceof \Closure) {
  117. $handle->setRender($class);
  118. }
  119. }
  120. }
  121. return $handle;
  122. }
  123. }