misonL commited on
Commit
64d4a03
·
1 Parent(s): 29305ed

feat: Add error handling and logging to status endpoint

Browse files

Wrap the `/status` route logic in a try-except block to handle potential errors gracefully.
Add logging for endpoint access, successful response data, and errors.
Return a 500 Internal Server Error response if an exception occurs while fetching status information.

Files changed (1) hide show
  1. app.py +36 -29
app.py CHANGED
@@ -374,35 +374,42 @@ def index():
374
  @app.route('/status')
375
  def get_system_status():
376
  """提供系统状态的 JSON 数据"""
377
- # 获取配置信息 (需要根据实际CONFIG结构调整)
378
- config_info = {
379
- "api_base_url": CONFIG["API"]["BASE_URL"],
380
- "log_level": logger.level, # 使用logger.level获取日志级别
381
- "is_temp_conversation": CONFIG["API"]["IS_TEMP_CONVERSATION"],
382
- "is_custom_sso": CONFIG["API"]["IS_CUSTOM_SSO"],
383
- "proxy": CONFIG["API"]["PROXY"],
384
- "manager_switch": CONFIG["ADMIN"]["MANAGER_SWITCH"],
385
- "show_thinking": CONFIG["SHOW_THINKING"],
386
- "isshow_search_results": CONFIG["ISSHOW_SEARCH_RESULTS"]
387
- }
388
-
389
- # 获取令牌状态
390
- token_status = {
391
- "total_tokens": token_manager.get_total_token_count(),
392
- "available_tokens": token_manager.get_remaining_token_request_capacity(),
393
- "total_request_count": token_manager.get_total_request_count(),
394
- "expired_tokens_count": len(token_manager.get_expired_tokens()),
395
- "token_details": token_manager.get_token_status_map() # 提供更详细的令牌状态
396
- }
397
-
398
- # 获取日志摘要
399
- log_summary = logger.get_recent_logs()
400
-
401
- return jsonify({
402
- "config": config_info,
403
- "token_status": token_status,
404
- "log_summary": log_summary
405
- })
 
 
 
 
 
 
 
406
 
407
 
408
  if __name__ == '__main__':
 
374
  @app.route('/status')
375
  def get_system_status():
376
  """提供系统状态的 JSON 数据"""
377
+ logger.info("访问 /status 路由", "StatusAPI") # 添加日志
378
+ try:
379
+ # 获取配置信息 (需要根据实际CONFIG结构调整)
380
+ config_info = {
381
+ "api_base_url": CONFIG["API"]["BASE_URL"],
382
+ "log_level": logger.level, # 使用logger.level获取日志级别
383
+ "is_temp_conversation": CONFIG["API"]["IS_TEMP_CONVERSATION"],
384
+ "is_custom_sso": CONFIG["API"]["IS_CUSTOM_SSO"],
385
+ "proxy": CONFIG["API"]["PROXY"],
386
+ "manager_switch": CONFIG["ADMIN"]["MANAGER_SWITCH"],
387
+ "show_thinking": CONFIG["SHOW_THINKING"],
388
+ "isshow_search_results": CONFIG["ISSHOW_SEARCH_RESULTS"]
389
+ }
390
+
391
+ # 获取令牌状态
392
+ token_status = {
393
+ "total_tokens": token_manager.get_total_token_count(),
394
+ "available_tokens": token_manager.get_remaining_token_request_capacity(),
395
+ "total_request_count": token_manager.get_total_request_count(),
396
+ "expired_tokens_count": len(token_manager.get_expired_tokens()),
397
+ "token_details": token_manager.get_token_status_map() # 提供更详细的令牌状态
398
+ }
399
+
400
+ # 获取日志摘要
401
+ log_summary = logger.get_recent_logs()
402
+
403
+ response_data = {
404
+ "config": config_info,
405
+ "token_status": token_status,
406
+ "log_summary": log_summary
407
+ }
408
+ logger.info(f"/status 路由返回数据: {json.dumps(response_data, indent=2)}", "StatusAPI") # 添加日志
409
+ return jsonify(response_data)
410
+ except Exception as e:
411
+ logger.error(f"访问 /status 路由时发生错误: {str(e)}", "StatusAPI") # 添加错误日志
412
+ return jsonify({"error": "无法获取系统状态"}), 500
413
 
414
 
415
  if __name__ == '__main__':