ExceptionHandle.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace app;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\ModelNotFoundException;
  14. use think\exception\Handle;
  15. use think\exception\HttpException;
  16. use think\exception\HttpResponseException;
  17. use think\exception\ValidateException;
  18. use think\Response;
  19. use Throwable;
  20. /**
  21. * 应用异常处理类
  22. */
  23. class ExceptionHandle extends Handle
  24. {
  25. /**
  26. * 不需要记录信息(日志)的异常类列表
  27. * @var array
  28. */
  29. protected $ignoreReport = [
  30. HttpException::class,
  31. HttpResponseException::class,
  32. ModelNotFoundException::class,
  33. DataNotFoundException::class,
  34. ValidateException::class,
  35. ];
  36. /**
  37. * 记录异常信息(包括日志或者其它方式记录)
  38. *
  39. * @access public
  40. * @param Throwable $exception
  41. * @return void
  42. */
  43. public function report(Throwable $exception): void
  44. {
  45. // 使用内置的方式记录异常日志
  46. parent::report($exception);
  47. }
  48. /**
  49. * Render an exception into an HTTP response.
  50. *
  51. * @access public
  52. * @param \think\Request $request
  53. * @param Throwable $e
  54. * @return Response
  55. */
  56. public function render($request, Throwable $e): Response
  57. {
  58. // 添加自定义异常处理机制
  59. // 其他错误交给系统处理
  60. return parent::render($request, $e);
  61. }
  62. }