datawhalechina/hello-agents · error · HTTPException
天气查询失败: {str(e)}
Error message
天气查询失败: {str(e)} What it means
HTTPException(500) raised by GET /map/weather in the chapter13 backend when service.get_weather(city) throws. Like the other map routes it wraps any exception from the Amap MCP tool chain (config, subprocess, network, upstream API) into a 500 with the cause message preserved.
Source
Thrown at code/chapter13/helloagents-trip-planner/backend/app/api/routes/map.py:93
Returns:
天气信息
"""
try:
# 获取服务实例
service = get_amap_service()
# 查询天气
weather_info = service.get_weather(city)
return WeatherResponse(
success=True,
message="天气查询成功",
data=weather_info
)
except Exception as e:
print(f"❌ 天气查询失败: {str(e)}")
raise HTTPException(
status_code=500,
detail=f"天气查询失败: {str(e)}"
)
@router.post(
"/route",
response_model=RouteResponse,
summary="规划路线",
description="规划两点之间的路线"
)
async def plan_route(request: RouteRequest):
"""
规划路线
Args:
request: 路线规划请求
View on GitHub (pinned to 606a07d341)
Solutions
- Check the server console for the '❌ 天气查询失败' line with the underlying message
- Validate AMAP_API_KEY and that other map endpoints (e.g. /map/poi/search) work — isolates weather-specific vs service-wide failure
- Test the Amap weather API directly with curl to rule out key/quota issues
- Ensure uvx and network access exist on the backend host
Example fix
# before
weather = requests.get(f'{BASE}/map/weather', params={'city': '北京'}).json()
# after
r = requests.get(f'{BASE}/map/weather', params={'city': '北京'})
if r.status_code >= 500:
print('Map service error detail:', r.json().get('detail'))
else:
weather = r.json() Defensive patterns
Strategy: try-catch
Validate before calling
import requests
if requests.get(f'{BASE}/map/health').status_code != 200:
raise SystemExit('Map service unhealthy')
# pass a mainstream city name to avoid upstream geocode errors
assert city.strip(), 'city required' Try / catch
try:
w = requests.get(f'{BASE}/map/weather', params={'city': city}, timeout=15)
w.raise_for_status()
except requests.HTTPError as e:
detail = e.response.json().get('detail')
if 'APIKEY' in str(detail) or 'USERKEY' in str(detail).upper():
alert_ops('Amap key invalid/expired')
raise Prevention
- Health-check the map service at startup, not per request
- Monitor for Amap quota errors in the detail messages and alert on them
- Set client timeouts — upstream hangs otherwise propagate as opaque 500s
When it happens
Trigger: weather MCP sub-tool missing because the amap server did not expand tools; invalid/missing AMAP_API_KEY; unsupported city name causing an upstream error surfaced as an exception; uvx subprocess failure on the host.
Common situations: Same environmental causes as the other /map routes: unset .env, missing uvx, quota-exhausted Amap key, or first-call timeout before the MCP server is warm.
Related errors
AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14).
Data as JSON: /api/errors/f3b771b90563979d.
Report an issue: GitHub.