🐛 Bug: Fix the bug where errors are not correctly returned when there is a syntax error in the configuration file.
Browse files
main.py
CHANGED
@@ -682,7 +682,17 @@ async def ensure_config(request: Request, call_next):
|
|
682 |
if len(app.state.api_keys_db) >= 1:
|
683 |
app.state.admin_api_key = app.state.api_keys_db[0].get("api")
|
684 |
else:
|
685 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
686 |
|
687 |
if app and not hasattr(app.state, 'client_manager'):
|
688 |
|
|
|
682 |
if len(app.state.api_keys_db) >= 1:
|
683 |
app.state.admin_api_key = app.state.api_keys_db[0].get("api")
|
684 |
else:
|
685 |
+
from utils import yaml_error_message
|
686 |
+
if yaml_error_message:
|
687 |
+
return JSONResponse(
|
688 |
+
status_code=500,
|
689 |
+
content={"error": yaml_error_message}
|
690 |
+
)
|
691 |
+
else:
|
692 |
+
return JSONResponse(
|
693 |
+
status_code=500,
|
694 |
+
content={"error": "No admin API key found"}
|
695 |
+
)
|
696 |
|
697 |
if app and not hasattr(app.state, 'client_manager'):
|
698 |
|
utils.py
CHANGED
@@ -246,6 +246,7 @@ yaml.preserve_quotes = True
|
|
246 |
yaml.indent(mapping=2, sequence=4, offset=2)
|
247 |
|
248 |
API_YAML_PATH = "./api.yaml"
|
|
|
249 |
|
250 |
def save_api_yaml(config_data):
|
251 |
with open(API_YAML_PATH, "w", encoding="utf-8") as f:
|
@@ -355,6 +356,8 @@ async def load_config(app=None):
|
|
355 |
config, api_keys_db, api_list = {}, {}, []
|
356 |
except YAMLError as e:
|
357 |
logger.error("配置文件 'api.yaml' 格式不正确。请检查 YAML 格式。%s", e)
|
|
|
|
|
358 |
config, api_keys_db, api_list = {}, {}, []
|
359 |
except OSError as e:
|
360 |
logger.error(f"open 'api.yaml' failed: {e}")
|
|
|
246 |
yaml.indent(mapping=2, sequence=4, offset=2)
|
247 |
|
248 |
API_YAML_PATH = "./api.yaml"
|
249 |
+
yaml_error_message = None
|
250 |
|
251 |
def save_api_yaml(config_data):
|
252 |
with open(API_YAML_PATH, "w", encoding="utf-8") as f:
|
|
|
356 |
config, api_keys_db, api_list = {}, {}, []
|
357 |
except YAMLError as e:
|
358 |
logger.error("配置文件 'api.yaml' 格式不正确。请检查 YAML 格式。%s", e)
|
359 |
+
global yaml_error_message
|
360 |
+
yaml_error_message = "配置文件 'api.yaml' 格式不正确。请检查 YAML 格式。"
|
361 |
config, api_keys_db, api_list = {}, {}, []
|
362 |
except OSError as e:
|
363 |
logger.error(f"open 'api.yaml' failed: {e}")
|