ExecptionHandle.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ 后台登录认证异常抛出 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: TABLE ME
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-25 16:10
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\warehouse\exception;
  13. use library\exceptions\AuthException;
  14. use library\exceptions\GeneralException;
  15. use think\db\exception\DbException;
  16. use think\exception\Handle;
  17. use think\Response;
  18. use Throwable;
  19. class ExecptionHandle extends Handle {
  20. /**
  21. * 记录异常信息(包括日志或者其它方式记录)
  22. *
  23. * @access public
  24. * @param Throwable $exception
  25. * @return void
  26. */
  27. public function report(Throwable $exception): void
  28. {
  29. // 使用内置的方式记录异常日志
  30. parent::report($exception);
  31. }
  32. /**
  33. * Render an exception into an HTTP response.
  34. *
  35. * @access public
  36. * @param \think\Request $request
  37. * @param Throwable $e
  38. * @return Response
  39. */
  40. public function render($request, Throwable $e): Response
  41. {
  42. // 添加自定义异常处理机制
  43. $massageData = env('app_debug', false) ? [
  44. 'file' => $e->getFile(),
  45. 'line' => $e->getLine(),
  46. 'trace' => $e->getTrace(),
  47. 'previous' => $e->getPrevious(),
  48. ] : [];
  49. if ($e instanceof DbException) {
  50. //记录服务器日志
  51. return app('json')->fail('抱歉,服务器数据异常!', ['line'=>$e->getMessage()]);
  52. }elseif ($e instanceof AuthException){
  53. return app('json')->make($e->getCode() ?: -1, $e->getMessage());
  54. } elseif ($e instanceof GeneralException) {
  55. return app('json')->make(-1, $e->getMessage());
  56. } else {
  57. return app('json')->make(-1, $e->getMessage());
  58. }
  59. }
  60. }