Skip to content

Commit 5eafa09

Browse files
committed
fix: enhance error handling and add cache headers for search responses
1 parent df6c8df commit 5eafa09

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

_search.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ async def keyword(request: Request):
163163
return JSONResponse({}, status_code=200)
164164
if _keyword == 'Yuki Forever💗':
165165
return JSONResponse(
166-
{"code": 0, "data": [{"type": "vod", "words": ["每一个未来的瞬间", "都有你的名字", "Yuki Forever💗"]}],
167-
"msg": "ok"}, status_code=200)
166+
{"code": 0, "data": [{"type": "vod", "words": ["pong"]}],
167+
"msg": "ok"}, status_code=200, headers={"X-Info": "Success"})
168168
redis_key = f"keyword_{datetime.datetime.now().strftime('%Y-%m-%d')}_{_keyword}"
169169
try:
170170
if await redis_get_key(redis_key):
@@ -176,11 +176,11 @@ async def keyword(request: Request):
176176
await redis_set_key(redis_key, json.dumps(data), ex=86400) # 缓存一天
177177
except Exception as e:
178178
logging.error("Error: " + str(e), stack_info=True)
179-
return JSONResponse({"error": str(e)}, status_code=501)
179+
return JSONResponse({"error": str(e)}, status_code=501, headers={"X-Error": str(e)})
180180
try:
181181
return JSONResponse(data)
182182
except:
183-
return JSONResponse(json.loads(data), status_code=200)
183+
return JSONResponse(json.loads(data), status_code=200, headers={"X-Cache": "MISS"})
184184

185185

186186
@searchRouter.api_route('/detail', methods=['POST'], name='detail',
@@ -192,7 +192,7 @@ async def detail(request: Request, background_tasks: BackgroundTasks):
192192
id = data.get('id')
193193
except Exception as e:
194194
return JSONResponse({"error": "Invalid Request, missing param: id"}, status_code=400,
195-
headers={"X-Error": str(e)})
195+
headers={"X-Cache": "MISS"})
196196

197197
# Try to return cached detail (cache for 30 minutes)
198198
redis_key = f"detail_{id}"
@@ -203,9 +203,9 @@ async def detail(request: Request, background_tasks: BackgroundTasks):
203203
# mark as cached so clients can know
204204
if isinstance(cached_data, dict):
205205
cached_data["msg"] = "cached"
206-
return JSONResponse(cached_data, status_code=200)
206+
return JSONResponse(cached_data, status_code=200, headers={"X-Cache": "HIT"})
207207
except Exception as e:
208-
logging.info(f"Invalid Request: {data}, {e}")
208+
logging.info(f"Invalid Request: {data}, {e}", stack_info=True)
209209
pass
210210
# if redis lookup fails, continue to fetch upstream
211211
vv = await generate_vv_detail()
@@ -228,9 +228,9 @@ async def detail(request: Request, background_tasks: BackgroundTasks):
228228
await redis_set_key(redis_key, json.dumps(response_data), ex=1800)
229229
except Exception:
230230
pass
231-
return JSONResponse(response_data, status_code=200)
231+
return JSONResponse(response_data, status_code=200, headers={"X-Cache": "MISS"})
232232
except Exception:
233-
return JSONResponse({"error": "Upstream Error"}, status_code=501)
233+
return JSONResponse({"error": "Upstream Error"}, status_code=501, headers={"X-Cache": "MISS, Upstream Error"})
234234

235235

236236
@searchRouter.api_route('/report/keyword', methods=['POST'], name='report_keyword',
@@ -252,5 +252,5 @@ async def report_keyword(request: Request):
252252
await redis_delete_key(key)
253253
except Exception as e:
254254
logging.error("Error: " + str(e), stack_info=True)
255-
return JSONResponse({"error": 'trace stack b1'}, status_code=501)
256-
return RedirectResponse(url='/api/query/ole/keyword', status_code=308)
255+
return JSONResponse({"error": 'trace stack b1'}, status_code=501, headers={"X-Error": str(e)})
256+
return RedirectResponse(url='/api/query/ole/keyword', status_code=308, headers={"X-Info": "Cache Purged, please re-query"})

0 commit comments

Comments
 (0)