ExceptionHandle.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare (strict_types=1);
  3. namespace app;
  4. use app\common\exception\ControllerExtendException;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\ModelNotFoundException;
  7. use think\exception\Handle;
  8. use think\exception\HttpException;
  9. use think\exception\HttpResponseException;
  10. use think\exception\ValidateException;
  11. use think\Response;
  12. use Throwable;
  13. /**
  14. * 应用异常处理类
  15. */
  16. class ExceptionHandle extends Handle
  17. {
  18. /**
  19. * 不需要记录信息(日志)的异常类列表
  20. * @var array
  21. */
  22. protected $ignoreReport = [
  23. HttpException::class,
  24. HttpResponseException::class,
  25. ModelNotFoundException::class,
  26. DataNotFoundException::class,
  27. ValidateException::class,
  28. ControllerExtendException::class,
  29. ];
  30. /**
  31. * 记录异常信息(包括日志或者其它方式记录)
  32. *
  33. * @access public
  34. * @param Throwable $exception
  35. * @return void
  36. */
  37. public function report(Throwable $exception): void
  38. {
  39. // 使用内置的方式记录异常日志
  40. parent::report($exception);
  41. }
  42. /**
  43. * Render an exception into an HTTP response.
  44. *
  45. * @access public
  46. * @param \think\Request $request
  47. * @param Throwable $e
  48. * @return Response
  49. */
  50. public function render($request, Throwable $e): Response
  51. {
  52. // 添加自定义异常处理机制
  53. // 其他错误交给系统处理
  54. return parent::render($request, $e);
  55. }
  56. }