Http.php 1001 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * User:林志杰
  5. * Email:[email protected]
  6. * Motto:纵有疾风起,人生不言弃!
  7. * Time:2020/1/13 23:58
  8. */
  9. declare(strict_types=1);
  10. namespace app\api\exception;
  11. use think\exception\Handle;
  12. use think\Response;
  13. use Throwable;
  14. class Http extends Handle
  15. {
  16. public $httpStatus = 500;
  17. /**
  18. * Render an exception into an HTTP response.
  19. *
  20. * @access public
  21. * @param \think\Request $request
  22. * @param Throwable $e
  23. * @return Response
  24. */
  25. public function render($request, Throwable $e): Response
  26. {
  27. if ($e instanceof \think\Exception) {
  28. return show($e->getCode(), $e->getMessage());
  29. }
  30. if (method_exists($e, "getStatusCode")) {
  31. $httpStatus = $e->getStatusCode();
  32. } else {
  33. $httpStatus = $this->httpStatus;
  34. }
  35. // 自定义异常处理机制
  36. return show(config('status.error'), $e->getMessage(), [], $httpStatus);
  37. }
  38. }