Skip to content

Commit df6c8df

Browse files
committed
fix: improve error handling and logging for upstream API requests
1 parent 1f52bc4 commit df6c8df

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

_search.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,31 +204,33 @@ async def detail(request: Request, background_tasks: BackgroundTasks):
204204
if isinstance(cached_data, dict):
205205
cached_data["msg"] = "cached"
206206
return JSONResponse(cached_data, status_code=200)
207-
except Exception:
208-
# if redis lookup fails, continue to fetch upstream
209-
vv = await generate_vv_detail()
210-
url = f"https://api.olelive.com/v1/pub/vod/detail/{id}/true?_vv={vv}"
211-
headers = {
207+
except Exception as e:
208+
logging.info(f"Invalid Request: {data}, {e}")
209+
pass
210+
# if redis lookup fails, continue to fetch upstream
211+
vv = await generate_vv_detail()
212+
url = f"https://api.olelive.com/v1/pub/vod/detail/{id}/true?_vv={vv}"
213+
headers = {
212214
'User-Agent': _getRandomUserAgent(),
213215
'Referer': 'https://www.olevod.com/',
214216
'Origin': 'https://www.olevod.com/',
215-
}
217+
}
218+
try:
219+
async with httpx.AsyncClient() as client:
220+
response = await client.get(url, headers=headers)
221+
response_data = response.json()
222+
# cache the response for 30 minutes (1800 seconds) in background
216223
try:
217-
async with httpx.AsyncClient() as client:
218-
response = await client.get(url, headers=headers)
219-
response_data = response.json()
220-
# cache the response for 30 minutes (1800 seconds) in background
224+
background_tasks.add_task(redis_set_key, redis_key, json.dumps(response_data), ex=1800)
225+
except Exception:
226+
# if scheduling background task fails, attempt immediate set but don't block on errors
221227
try:
222-
background_tasks.add_task(redis_set_key, redis_key, json.dumps(response_data), ex=1800)
228+
await redis_set_key(redis_key, json.dumps(response_data), ex=1800)
223229
except Exception:
224-
# if scheduling background task fails, attempt immediate set but don't block on errors
225-
try:
226-
await redis_set_key(redis_key, json.dumps(response_data), ex=1800)
227-
except Exception:
228-
pass
229-
return JSONResponse(response_data, status_code=200)
230-
except Exception:
231-
return JSONResponse({"error": "Upstream Error"}, status_code=501)
230+
pass
231+
return JSONResponse(response_data, status_code=200)
232+
except Exception:
233+
return JSONResponse({"error": "Upstream Error"}, status_code=501)
232234

233235

234236
@searchRouter.api_route('/report/keyword', methods=['POST'], name='report_keyword',

0 commit comments

Comments
 (0)