Skip to content

Commit

Permalink
加入更多ipapi错误处理
Browse files Browse the repository at this point in the history
  • Loading branch information
GaiZhenbiao committed Mar 22, 2023
1 parent 519f524 commit d1c8d88
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,19 @@ def replace_today(prompt):

def get_geoip():
response = requests.get('https://ipapi.co/json/', timeout=5)
data = response.json()
try:
data = response.json()
except:
data = {
"error": True,
"reason" : "连接ipapi失败"
}
if "error" in data.keys():
logging.warning(f"无法获取IP地址信息。\n{data}")
if data['reason'] == "RateLimited":
return f"获取IP地理位置失败,因为达到了检测IP的速率限制。聊天功能可能仍然可用,但请注意,如果您的IP地址在不受支持的地区,您可能会遇到问题。"
else:
return f"获取IP地理位置失败。原因:{data['reason']}"
return f"获取IP地理位置失败。原因:{data['reason']}。你仍然可以使用聊天功能。"
else:
country = data['country_name']
if country == "China":
Expand Down

0 comments on commit d1c8d88

Please sign in to comment.