KefuApiExceptionHandle.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\kefuapi;
  12. use crmeb\exceptions\AdminException;
  13. use crmeb\exceptions\ApiException;
  14. use crmeb\exceptions\AuthException;
  15. use think\db\exception\DbException;
  16. use think\exception\Handle;
  17. use think\exception\ValidateException;
  18. use think\facade\Env;
  19. use think\facade\Log;
  20. use think\Response;
  21. use Throwable;
  22. class KefuApiExceptionHandle extends Handle
  23. {
  24. /**
  25. * 不需要记录信息(日志)的异常类列表
  26. * @var array
  27. */
  28. protected $ignoreReport = [
  29. ValidateException::class,
  30. AuthException::class,
  31. AdminException::class,
  32. ApiException::class,
  33. ];
  34. /**
  35. * 记录异常信息(包括日志或者其它方式记录)
  36. * @access public
  37. * @param Throwable $exception
  38. * @return void
  39. */
  40. public function report(Throwable $exception): void
  41. {
  42. if (!$this->isIgnoreReport($exception)) {
  43. $data = [
  44. 'file' => $exception->getFile(),
  45. 'line' => $exception->getLine(),
  46. 'message' => $this->getMessage($exception),
  47. 'code' => $this->getCode($exception),
  48. ];
  49. //日志内容
  50. $log = [
  51. request()->kefuId(), //客服ID
  52. request()->ip(), //客户ip
  53. ceil(msectime() - (request()->time(true) * 1000)), //耗时(毫秒)
  54. request()->rule()->getMethod(), //请求类型
  55. str_replace("/", "", request()->rootUrl()), //应用
  56. request()->baseUrl(), //路由
  57. json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //请求参数
  58. json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //报错数据
  59. ];
  60. Log::write(implode("|", $log), "error");
  61. }
  62. }
  63. /**
  64. * Render an exception into an HTTP response.
  65. * @access public
  66. * @param \think\Request $request
  67. * @param Throwable $e
  68. * @return Response
  69. */
  70. public function render($request, Throwable $e): Response
  71. {
  72. $massageData = Env::get('app_debug', false) ? [
  73. 'message' => $e->getMessage(),
  74. 'file' => $e->getFile(),
  75. 'line' => $e->getLine(),
  76. 'trace' => $e->getTrace(),
  77. 'previous' => $e->getPrevious(),
  78. ] : [];
  79. $message = $e->getMessage();
  80. // 添加自定义异常处理机制
  81. if ($e instanceof AuthException || $e instanceof AdminException || $e instanceof ApiException || $e instanceof ValidateException) {
  82. return app('json')->make($e->getCode() ?: 400, $message, $massageData);
  83. } else {
  84. return app('json')->fail($message, $massageData);
  85. }
  86. }
  87. }