ApiExceptionHandle.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\ApiException;
  13. use crmeb\exceptions\AuthException;
  14. use think\exception\DbException;
  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. * @access public
  26. * @param Throwable $exception
  27. * @return void
  28. */
  29. public function report(Throwable $exception):void
  30. {
  31. $data = [
  32. 'file' => $exception->getFile(),
  33. 'line' => $exception->getLine(),
  34. 'message' => $this->getMessage($exception),
  35. 'code' => $this->getCode($exception),
  36. ];
  37. //日志内容
  38. $log = [
  39. request()->uid(), //用户ID
  40. request()->ip(), //客户ip
  41. ceil(msectime() - (request()->time(true) * 1000)), //耗时(毫秒)
  42. request()->rule()->getMethod(), //请求类型
  43. str_replace("/", "", request()->rootUrl()), //应用
  44. request()->baseUrl(), //路由
  45. json_encode(request()->param(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //请求参数
  46. json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), //报错数据
  47. ];
  48. Log::write(implode("|", $log), "error");
  49. }
  50. /**
  51. * Render an exception into an HTTP response.
  52. * @access public
  53. * @param \think\Request $request
  54. * @param Throwable $e
  55. * @return Response
  56. */
  57. public function render($request, Throwable $e):Response
  58. {
  59. // 添加自定义异常处理机制
  60. if ($e instanceof DbException) {
  61. return app('json')->fail('数据获取失败', [
  62. 'file' => $e->getFile(),
  63. 'message' => $e->getMessage(),
  64. 'line' => $e->getLine(),
  65. ]);
  66. } elseif ($e instanceof AuthException || $e instanceof ApiException || $e instanceof ValidateException) {
  67. return app('json')->fail($e->getMessage());
  68. } else {
  69. return app('json')->fail('很抱歉!系统开小差了', Env::get('app_debug', false) ? [
  70. 'message' => $e->getMessage(),
  71. 'file' => $e->getFile(),
  72. 'code' => $e->getCode(),
  73. 'line' => $e->getLine(),
  74. 'trace' => $e->getTrace(),
  75. 'previous' => $e->getPrevious(),
  76. ] : []);
  77. }
  78. }
  79. }