AppserverErrorHandler.php 4.3 KB

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