ApiExceptionHandle.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\api;
  12. use crmeb\exceptions\AdminException;
  13. use crmeb\exceptions\ApiException;
  14. use crmeb\exceptions\ApiStatusException;
  15. use crmeb\exceptions\AuthException;
  16. use think\db\exception\DbException;
  17. use think\exception\Handle;
  18. use think\facade\Env;
  19. use think\facade\Log;
  20. use think\Response;
  21. use Throwable;
  22. use think\exception\ValidateException;
  23. class ApiExceptionHandle extends Handle
  24. {
  25. /**
  26. * 不需要记录信息(日志)的异常类列表
  27. * @var array
  28. */
  29. protected $ignoreReport = [
  30. ValidateException::class,
  31. AuthException::class,
  32. AdminException::class,
  33. ApiException::class,
  34. ];
  35. /**
  36. * 记录异常信息(包括日志或者其它方式记录)
  37. * @access public
  38. * @param Throwable $exception
  39. * @return void
  40. */
  41. public function report(Throwable $exception): void
  42. {
  43. if (!$this->isIgnoreReport($exception)) {
  44. $data = [
  45. 'file' => $exception->getFile(),
  46. 'line' => $exception->getLine(),
  47. 'message' => $this->getMessage($exception),
  48. 'code' => $this->getCode($exception),
  49. ];
  50. //日志内容
  51. $log = [
  52. request()->uid(), //用户ID
  53. request()->ip(), //客户ip
  54. ceil(msectime() - (request()->time(true) * 1000)), //耗时(毫秒)
  55. request()->rule()->getMethod(), //请求类型
  56. str_replace("/", "", request()->rootUrl()), //应用
  57. request()->baseUrl(), //路由
  58. json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),//请求参数
  59. json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //报错数据
  60. ];
  61. Log::write(implode("|", $log), "error");
  62. }
  63. }
  64. /**
  65. * Render an exception into an HTTP response.
  66. * @access public
  67. * @param \think\Request $request
  68. * @param Throwable $e
  69. * @return Response
  70. */
  71. public function render($request, Throwable $e): Response
  72. {
  73. $massageData = Env::get('app_debug', false) ? [
  74. 'message' => $e->getMessage(),
  75. 'file' => $e->getFile(),
  76. 'line' => $e->getLine(),
  77. 'trace' => $e->getTrace(),
  78. 'previous' => $e->getPrevious(),
  79. ] : [];
  80. $message = $e->getMessage();
  81. // 添加自定义异常处理机制
  82. if ($e instanceof ApiStatusException) {
  83. return app('json')->status($e->getApiStatus(), $message, $e->getApiData());
  84. }
  85. if ($e instanceof AuthException || $e instanceof AdminException || $e instanceof ApiException || $e instanceof ValidateException) {
  86. return app('json')->make($e->getCode() ?: 400, $message, $massageData);
  87. }
  88. return app('json')->fail($message, $massageData);
  89. }
  90. }