OutApiExceptionHandle.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\outapi;
  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\facade\Env;
  18. use think\facade\Log;
  19. use think\Response;
  20. use Throwable;
  21. use think\exception\ValidateException;
  22. class OutApiExceptionHandle 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()->uid(), //用户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. }