AppfrontErrorHandler.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace fecshop\components;
  3. use Yii;
  4. use yii\web\ErrorHandler;
  5. use yii\base\Exception;
  6. /**
  7. * 异常捕获器
  8. */
  9. class AppfrontErrorHandler extends ErrorHandler
  10. {
  11. /**
  12. * [renderException description]
  13. * @param $exception | Object 异常数据对象
  14. *
  15. */
  16. public function renderException($exception)
  17. {
  18. //echo 1;exit;
  19. // 获取异常数据 , 404页面不做收集
  20. $code = $exception->statusCode ?: 500;
  21. if ($code != 404) {
  22. method_exists($exception,'getMessage') ? $message = $exception->getMessage() : $message = '';
  23. method_exists($exception,'getName') ? $name = $exception->getName() : $name = '';
  24. method_exists($exception,'getFile') ? $file = $exception->getFile() : $file = '';
  25. method_exists($exception,'getLine') ? $line = $exception->getLine() : $line = '';
  26. method_exists($exception,'getTraceAsString') ? $traceString = $exception->getTraceAsString() : $traceString = '';
  27. $time = time();
  28. $ip = Yii::$app->request->userIP;
  29. $url = Yii::$service->url->getCurrentUrl();
  30. $req_info = $this->getRequestInfo();
  31. $response = Yii::$app->response;
  32. Yii::$app->response->format = $response::FORMAT_JSON;
  33. if (YII_ENV_PROD) {
  34. $errorKey = $this->saveProdException($code, $message, $file, $line, $time, $ip, $name, $traceString, $url, $req_info);
  35. Yii::$app->response->data = [
  36. 'code' => $code,
  37. 'error_no' => $errorKey,
  38. ];
  39. Yii::$app->response->send();
  40. Yii::$app->end();
  41. } else {
  42. $time = date('Y-m-d H:i:s', $time);
  43. $exceptionInfo = [
  44. 'code' => $code,
  45. 'message' => $message,
  46. 'file' => $file,
  47. 'line' => $line,
  48. 'time' => $time,
  49. 'ip' => $ip,
  50. 'name' => $name,
  51. 'traceString' => $traceString,
  52. ];
  53. Yii::$app->response->data = $exceptionInfo;
  54. Yii::$app->response->send();
  55. Yii::$app->end();
  56. }
  57. } else {
  58. parent::renderException($exception);
  59. }
  60. }
  61. public function getRequestInfo(){
  62. $request = Yii::$app->request;
  63. $ajax = 0;
  64. $request_type = '';
  65. $request_data = [];
  66. $header_accept = '';
  67. $header_user_agent = '';
  68. if ($request->isAjax) {
  69. $ajax = 1;
  70. }
  71. if ($request->isGet) {
  72. $request_type = 'get';
  73. $request_data = $request->get();
  74. }
  75. if ($request->isPost) {
  76. $request_type = 'post';
  77. $request_data = $request->post();
  78. }
  79. // $headers is an object of yii\web\HeaderCollection
  80. $headers = $request->getHeaders();
  81. $headers_arr = [];
  82. if (is_object($headers) or is_array($headers)) {
  83. foreach ($headers as $k=>$v) {
  84. $headers_arr[$k] = $v;
  85. }
  86. }
  87. $userHost = Yii::$app->request->userHost;
  88. $userIP = Yii::$app->request->userIP;
  89. return [
  90. 'ajax' => $ajax,
  91. 'request_type' => $request_type,
  92. 'request_data' => $request_data,
  93. 'headers_data' => $headers_arr,
  94. 'userHost' => $userHost,
  95. 'userIP' => $userIP,
  96. ];
  97. }
  98. public function saveProdException($code, $message, $file, $line, $created_at, $ip, $name, $trace_string, $url, $req_info){
  99. return Yii::$service->helper->errorHandler->saveByErrorHandler(
  100. $code, $message, $file, $line, $created_at,
  101. $ip, $name, $trace_string, $url, $req_info
  102. );
  103. }
  104. /**
  105. * 这块代码目前没有编写,您也可以用 Sentry(错误日志收集框架) 来收集错误日志。
  106. * $option = [
  107. * 'fromMail' => 'xxx@xxx.com',
  108. * 'subject' => 'fecshop报错 Code:' . $exceptionInfo['code'],
  109. * 'htmlBody' => '异常' . '(' . YII_ENV . ')' . $exceptionInfo['code'] . ':' . $exceptionInfo['message'] . '<br />文件:' . $exceptionInfo['file'] . ':' . $exceptionInfo['line'] . '<br /> 时间:' . $exceptionInfo['time'] . '<br />请求ip:' . $exceptionInfo['ip'],
  110. * ];
  111. *
  112. *
  113. *if (!$result) {
  114. * throw new Exception("Exception mail send faild!", 1);
  115. *}
  116. */
  117. }