AdminApiExceptionHandle.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\ValidateException;
  18. use think\facade\Env;
  19. use think\facade\Log;
  20. use think\Response;
  21. use Throwable;
  22. class AdminApiExceptionHandle 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. try {
  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()->adminId(), //管理员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. } catch (\Throwable $e) {
  63. Log::write($e->getMessage(), "error");
  64. }
  65. }
  66. }
  67. /**
  68. * Render an exception into an HTTP response.
  69. * @access public
  70. * @param \think\Request $request
  71. * @param Throwable $e
  72. * @return Response
  73. */
  74. public function render($request, Throwable $e): Response
  75. {
  76. $massageData = Env::get('app_debug', false) ? [
  77. 'message' => $e->getMessage(),
  78. 'file' => $e->getFile(),
  79. 'line' => $e->getLine(),
  80. 'trace' => $e->getTrace(),
  81. 'previous' => $e->getPrevious(),
  82. ] : [];
  83. $message = $e->getMessage();
  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. } else {
  88. return app('json')->fail($message, $massageData);
  89. }
  90. }
  91. }