AdminApiExceptionHandle.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. DbException::class,//数据库错误
  32. AuthException::class,//权限错误
  33. AdminException::class,//后台错误
  34. ApiException::class,//接口错误
  35. ];
  36. /**
  37. * 记录异常信息(包括日志或者其它方式记录)
  38. * @access public
  39. * @param Throwable $exception
  40. * @return void
  41. */
  42. public function report(Throwable $exception): void
  43. {
  44. if (!$this->isIgnoreReport($exception)) {
  45. try {
  46. $data = [
  47. 'file' => $exception->getFile(),//文件
  48. 'line' => $exception->getLine(),//行数
  49. 'message' => $this->getMessage($exception),//错误信息
  50. 'code' => $this->getCode($exception),//错误码
  51. ];
  52. //日志内容
  53. $log = [
  54. request()->adminId(), //管理员ID
  55. request()->ip(), //客户ip
  56. ceil(msectime() - (request()->time(true) * 1000)), //耗时(毫秒)
  57. request()->rule()->getMethod(), //请求类型
  58. str_replace("/", "", request()->rootUrl()), //应用
  59. request()->baseUrl(), //路由
  60. json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),//请求参数
  61. json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //报错数据
  62. ];
  63. Log::write(implode("|", $log), "error");//记录日志
  64. } catch (\Throwable $e) {
  65. Log::write($e->getMessage(), "error");//记录日志
  66. }
  67. }
  68. }
  69. /**
  70. * Render an exception into an HTTP response.
  71. * @access public
  72. * @param \think\Request $request
  73. * @param Throwable $e
  74. * @return Response
  75. */
  76. public function render($request, Throwable $e): Response
  77. {
  78. // 如果是响应异常,直接返回
  79. if ($e instanceof HttpResponseException) {
  80. return parent::render($request, $e);//直接返回
  81. }
  82. // 调试模式下返回异常信息
  83. $massageData = Env::get('app_debug', false) ? [
  84. 'message' => $e->getMessage(),//错误信息
  85. 'file' => $e->getFile(),//文件
  86. 'line' => $e->getLine(),//行数
  87. 'trace' => $e->getTrace(),// 异常的追踪
  88. 'previous' => $e->getPrevious(),// 异常的上一个异常
  89. ] : [];
  90. $message = $e->getMessage();
  91. // 添加自定义异常处理机制
  92. if ($e instanceof AuthException || $e instanceof AdminException || $e instanceof ApiException || $e instanceof ValidateException) {
  93. return app('json')->make($e->getCode() ?: 400, $message, $massageData);
  94. } else {
  95. return app('json')->fail($message, $massageData);//返回错误信息
  96. }
  97. }
  98. }