Http.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 ($e instanceof \think\exception\HttpResponseException) {
  31. return parent::render($request, $e);
  32. }
  33. if (method_exists($e, "getStatusCode")) {
  34. $httpStatus = $e->getStatusCode();
  35. } else {
  36. $httpStatus = $this->httpStatus;
  37. }
  38. // 自定义异常处理机制
  39. return show(config('status.error'), $e->getMessage(), [], $httpStatus);
  40. }
  41. }