ExceptionHandle.php 1.4 KB

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