AdminApiExceptionHandle.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\adminapi;
  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\HttpResponseException;
  18. use think\exception\ValidateException;
  19. use think\facade\Env;
  20. use think\facade\Log;
  21. use think\Response;
  22. use Throwable;
  23. class AdminApiExceptionHandle 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. try {
  45. $data = [
  46. 'file' => $exception->getFile(),
  47. 'line' => $exception->getLine(),
  48. 'message' => $this->getMessage($exception),
  49. 'code' => $this->getCode($exception),
  50. ];
  51. //日志内容
  52. $log = [
  53. request()->adminId(), //管理员ID
  54. request()->ip(), //客户ip
  55. ceil(msectime() - (request()->time(true) * 1000)), //耗时(毫秒)
  56. request()->rule()->getMethod(), //请求类型
  57. str_replace("/", "", request()->rootUrl()), //应用
  58. request()->baseUrl(), //路由
  59. json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),//请求参数
  60. json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //报错数据
  61. ];
  62. Log::write(implode("|", $log), "error");
  63. } catch (\Throwable $e) {
  64. Log::write($e->getMessage(), "error");
  65. }
  66. }
  67. }
  68. /**
  69. * Render an exception into an HTTP response.
  70. * @access public
  71. * @param \think\Request $request
  72. * @param Throwable $e
  73. * @return Response
  74. */
  75. public function render($request, Throwable $e): Response
  76. {
  77. if ($e instanceof HttpResponseException) {
  78. return parent::render($request, $e);
  79. }
  80. $massageData = Env::get('app_debug', false) ? [
  81. 'message' => $e->getMessage(),
  82. 'file' => $e->getFile(),
  83. 'line' => $e->getLine(),
  84. 'trace' => $e->getTrace(),
  85. 'previous' => $e->getPrevious(),
  86. ] : [];
  87. $message = $e->getMessage();
  88. // 添加自定义异常处理机制
  89. if ($e instanceof AuthException || $e instanceof AdminException || $e instanceof ApiException || $e instanceof ValidateException) {
  90. return app('json')->make($e->getCode() ?: 400, $message, $massageData);
  91. } else {
  92. return app('json')->fail($message, $massageData);
  93. }
  94. }
  95. }