Http.php 790 B

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace app\common\exception;
  3. use Exception;
  4. use think\exception\Handle;
  5. use think\exception\HttpException;
  6. use think\exception\ValidateException;
  7. class Http extends Handle
  8. {
  9. public function render(Exception $e)
  10. {
  11. $json = [
  12. 'code' => 1,
  13. 'message' => '',
  14. 'data' => []
  15. ];
  16. // 参数验证错误
  17. if ($e instanceof ValidateException) {
  18. $json['message'] = $e->getError();
  19. return json($json);
  20. }
  21. // 请求异常
  22. if ($e instanceof HttpException && request()->isAjax()) {
  23. $json['message'] = $e->getMessage();
  24. return json($json,$e->getStatusCode());
  25. }
  26. $json['message'] = $e->getMessage();
  27. return json($json);
  28. }
  29. }