ApiExceptionHandle.php 3.8 KB

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