ExceptionHandle.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\ModelNotFoundException;
  14. use think\db\exception\PDOException;
  15. use think\exception\ErrorException;
  16. use think\exception\Handle;
  17. use think\exception\HttpException;
  18. use think\exception\HttpResponseException;
  19. use think\exception\ValidateException;
  20. use think\Response;
  21. use Throwable;
  22. /**
  23. * 应用异常处理类
  24. */
  25. class ExceptionHandle extends Handle
  26. {
  27. /**
  28. * 不需要记录信息(日志)的异常类列表
  29. * @var array
  30. */
  31. protected $ignoreReport = [
  32. HttpException::class,
  33. HttpResponseException::class,
  34. ModelNotFoundException::class,
  35. DataNotFoundException::class,
  36. ValidateException::class,
  37. ];
  38. /**
  39. * 记录异常信息(包括日志或者其它方式记录)
  40. *
  41. * @access public
  42. * @param Throwable $exception
  43. * @return void
  44. */
  45. public function report(Throwable $exception): void
  46. {
  47. // 使用内置的方式记录异常日志
  48. parent::report($exception);
  49. }
  50. /**
  51. * Render an exception into an HTTP response.
  52. *
  53. * @access public
  54. * @param \think\Request $request
  55. * @param Throwable $e
  56. * @return Response
  57. */
  58. public function render($request, Throwable $e): Response
  59. {
  60. // 添加自定义异常处理机制
  61. $this->report($e);
  62. // 其他错误交给系统处理
  63. if ($e instanceof ValidateException)
  64. return app('json')->fail($e->getMessage());
  65. else if ($e instanceof DataNotFoundException)
  66. return app('json')->fail(isDebug() ? $e->getMessage() : '数据不存在');
  67. else if ($e instanceof ModelNotFoundException)
  68. return app('json')->fail(isDebug() ? $e->getMessage() : '数据不存在');
  69. else if ($e instanceof PDOException)
  70. return app('json')->fail(isDebug() ? $e->getMessage() : '数据库操作失败', isDebug() ? $e->getData() : []);
  71. else if ($e instanceof ErrorException)
  72. return app('json')->fail(isDebug() ? $e->getMessage() : '系统错误', isDebug() ? $e->getData() : []);
  73. else if ($e instanceof \PDOException)
  74. return app('json')->fail(isDebug() ? $e->getMessage() : '数据库连接失败');
  75. else if ($e instanceof \EasyWeChat\Core\Exceptions\HttpException)
  76. return app('json')->fail($e->getMessage());
  77. return parent::render($request, $e);
  78. }
  79. }