namespace Admin.NET.Plugin.AiDOP.DataPlatform;
///
/// 把数据库驱动异常转换为可操作且不泄露主机、账号、连接串的页面提示。
///
internal static class MdpDbConnectionError
{
internal static (string Code, string Message) Describe(Exception exception)
{
var text = Flatten(exception);
if (exception is MdpSourceSecretException || Contains(text, "SECRET_MISSING"))
return ("SECRET_MISSING", "数据库口令未配置,请在数据源编辑页重新填写并保存");
if (Contains(text, "Access denied", "Login failed", "authentication failed", "password authentication failed"))
return ("AUTH_FAILED", "数据库账号、口令或来源地址授权不正确,请核对只读账号及应用服务器访问授权");
if (Contains(text, "Unknown database", "Cannot open database", "database does not exist"))
return ("DATABASE_NOT_FOUND", "目标数据库不存在或当前账号无权访问");
if (exception is TimeoutException
|| Contains(text, "timed out", "timeout", "ETIMEDOUT", "Unable to connect", "actively refused", "Connection refused"))
return ("CONNECTION_FAILED", "数据库网络连接失败,请检查主机、端口、防火墙和访问白名单");
return ("CONNECTION_FAILED", "数据库连接失败,请检查连接配置并查看服务端日志");
}
private static string Flatten(Exception exception)
{
var messages = new List();
for (var current = exception; current != null; current = current.InnerException)
messages.Add(current.Message);
return string.Join(" | ", messages);
}
private static bool Contains(string text, params string[] values) =>
values.Any(value => text.Contains(value, StringComparison.OrdinalIgnoreCase));
}