ExceptionHandle.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\facade\Log;
  19. use think\Response;
  20. use Throwable;
  21. /**
  22. * 应用异常处理类
  23. */
  24. class ExceptionHandle extends Handle
  25. {
  26. /**
  27. * 不需要记录信息(日志)的异常类列表
  28. * @var array
  29. */
  30. protected $ignoreReport = [
  31. HttpException::class,
  32. HttpResponseException::class,
  33. // ModelNotFoundException::class,
  34. // DataNotFoundException::class,
  35. ValidateException::class,
  36. ];
  37. /**
  38. * 记录异常信息(包括日志或者其它方式记录)
  39. *
  40. * @access public
  41. * @param Throwable $exception
  42. * @return void
  43. */
  44. public function report(Throwable $exception): void
  45. {
  46. // 使用内置的方式记录异常日志
  47. Log::write($exception->getMessage().'||'.$exception->getFile().'||'.$exception->getLine().'||'.$exception->getTraceAsString(),'error');
  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. // 其他错误交给系统处理
  62. Log::write($e->getMessage().'||'.$e->getFile().'||'.$e->getLine().'||'.$e->getTraceAsString(),'error');
  63. return parent::render($request, $e);
  64. }
  65. }