Refine MySQL status messaging and SQL page copy
This commit is contained in:
+22
-2
@@ -214,6 +214,20 @@ def run_command(args: list[str]) -> subprocess.CompletedProcess[str]:
|
||||
)
|
||||
|
||||
|
||||
def sanitize_mysql_message(message: str, host: str | None = None, port: str | None = None) -> str:
|
||||
lines = [line.strip() for line in str(message or "").splitlines() if line.strip()]
|
||||
filtered = [line for line in lines if not line.startswith("WARNING:")]
|
||||
text = "\n".join(filtered or lines)
|
||||
host_text = host or os.environ.get("MYSQL_HOST", "mysql")
|
||||
port_text = port or os.environ.get("MYSQL_PORT", "3306")
|
||||
|
||||
if "Can't connect to server on" in text or "Can't connect to MySQL server on" in text:
|
||||
return f"MySQL 当前无法连接: {host_text}:{port_text}"
|
||||
if "Access denied" in text:
|
||||
return f"MySQL 账号或密码无效: {host_text}:{port_text}"
|
||||
return text or f"MySQL 当前无法连接: {host_text}:{port_text}"
|
||||
|
||||
|
||||
def normalize_text(text: str) -> str:
|
||||
return NORMALIZE_RE.sub("", (text or "").lower())
|
||||
|
||||
@@ -255,7 +269,9 @@ def run_mysql_query(sql: str, database: str | None = None) -> list[dict[str, str
|
||||
check=False,
|
||||
)
|
||||
if proc.returncode != 0:
|
||||
message = proc.stderr.strip() or proc.stdout.strip() or f"mysql exited with {proc.returncode}"
|
||||
message = sanitize_mysql_message(
|
||||
proc.stderr.strip() or proc.stdout.strip() or f"mysql exited with {proc.returncode}"
|
||||
)
|
||||
raise RuntimeError(message)
|
||||
|
||||
lines = [line for line in proc.stdout.splitlines() if line.strip()]
|
||||
@@ -369,7 +385,11 @@ def get_status_payload() -> dict[str, object]:
|
||||
mysql_ready = True
|
||||
mysql_status = mysql_proc.stdout.strip() or "MySQL ready"
|
||||
else:
|
||||
mysql_status = mysql_proc.stderr.strip() or mysql_proc.stdout.strip() or "MySQL unavailable"
|
||||
mysql_status = sanitize_mysql_message(
|
||||
mysql_proc.stderr.strip() or mysql_proc.stdout.strip() or "MySQL unavailable",
|
||||
host=mysql_host,
|
||||
port=mysql_port,
|
||||
)
|
||||
else:
|
||||
mysql_status = "MySQL auto load disabled"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user