fix: 安装页测试连接失败时向前端返回真实错误原因
This commit is contained in:
@@ -6,7 +6,9 @@ import { execFileSync } from 'child_process';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import { PrismaClient, RoleType, UserStatus } from '@prisma/client';
|
||||
import Redis from 'ioredis';
|
||||
import { ErrorCode } from '@artveris/shared';
|
||||
import { envFilePath, isInstallMode } from '../config/runtime-config';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
import { InstallRequestDto, InstallStorageDto, TestConnectionDto } from './dto/install.dto';
|
||||
import { LocalDriver } from '../storage/local.driver';
|
||||
import { OssDriver } from '../storage/oss.driver';
|
||||
@@ -54,7 +56,7 @@ export class InstallService {
|
||||
async testConnection(dto: TestConnectionDto): Promise<{ success: true; message: string }> {
|
||||
// 仅安装模式可用,避免安装完成后被当作公开的内网端口探测工具
|
||||
if (!isInstallMode()) {
|
||||
throw new Error('系统已安装,连接测试不可用');
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, '系统已安装,连接测试不可用');
|
||||
}
|
||||
if (dto.target === 'redis') {
|
||||
const redis = new Redis({
|
||||
@@ -68,6 +70,8 @@ export class InstallService {
|
||||
try {
|
||||
await redis.connect();
|
||||
await redis.ping();
|
||||
} catch (e) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, `Redis 连接失败:${this.errorMessage(e)}`);
|
||||
} finally {
|
||||
redis.disconnect();
|
||||
}
|
||||
@@ -92,25 +96,32 @@ export class InstallService {
|
||||
return { success: true, message: 'MySQL 连接成功,业务库已存在' };
|
||||
} catch (e) {
|
||||
const code = (e as { code?: string }).code;
|
||||
const msg = (e as Error).message || '';
|
||||
const msg = this.errorMessage(e);
|
||||
if (code === 'P1003' || /Unknown database|P1049|does not exist/i.test(msg)) {
|
||||
// 库不存在,但服务器可连接:再连 information_schema 确认凭据可用
|
||||
await tryConnect(`${base}/information_schema`);
|
||||
try {
|
||||
await tryConnect(`${base}/information_schema`);
|
||||
} catch (e2) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, `MySQL 连接失败:${this.errorMessage(e2)}`);
|
||||
}
|
||||
return { success: true, message: 'MySQL 连接成功,业务库不存在(安装时将自动创建)' };
|
||||
}
|
||||
throw new Error(`MySQL 连接失败:${msg}`);
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, `MySQL 连接失败:${msg}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 安装页「测试存储」:本地校验目录可写、OSS/S3 校验凭据可读写。只做健康检查、随后清理。
|
||||
async testStorage(dto: InstallStorageDto): Promise<{ success: true; message: string }> {
|
||||
if (!isInstallMode()) {
|
||||
throw new Error('系统已安装,存储测试不可用');
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, '系统已安装,存储测试不可用');
|
||||
}
|
||||
let driver: StorageDriver;
|
||||
if (dto.driver === 'oss') {
|
||||
if (!dto.ossBucket || !dto.ossAccessKeyId || !dto.ossAccessKeySecret) {
|
||||
throw new Error('OSS 配置不完整:Endpoint / Bucket / AccessKey ID / AccessKey Secret 均为必填');
|
||||
throw new BusinessException(
|
||||
ErrorCode.BAD_REQUEST,
|
||||
'OSS 配置不完整:Endpoint / Bucket / AccessKey ID / AccessKey Secret 均为必填',
|
||||
);
|
||||
}
|
||||
driver = new OssDriver({
|
||||
endpoint: dto.ossEndpoint || '',
|
||||
@@ -133,11 +144,15 @@ export class InstallService {
|
||||
await driver.get(key);
|
||||
await driver.remove(key);
|
||||
} catch (e) {
|
||||
throw new Error(`存储测试失败:${(e as Error).message}`);
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, `存储测试失败:${this.errorMessage(e)}`);
|
||||
}
|
||||
return { success: true, message: dto.driver === 'oss' ? 'OSS/S3 读写测试通过' : '本地目录读写测试通过' };
|
||||
}
|
||||
|
||||
private errorMessage(e: unknown): string {
|
||||
return (e as Error)?.message || String(e);
|
||||
}
|
||||
|
||||
private buildMysqlUrl(dto: InstallRequestDto): string {
|
||||
const user = encodeURIComponent(dto.mysql.username);
|
||||
const password = encodeURIComponent(dto.mysql.password);
|
||||
@@ -150,8 +165,10 @@ export class InstallService {
|
||||
try {
|
||||
await prisma.$connect();
|
||||
await prisma.$queryRaw`SELECT 1`;
|
||||
} catch (e) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, `MySQL 连接失败:${this.errorMessage(e)}`);
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
await prisma.$disconnect().catch(() => undefined);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -159,7 +176,7 @@ export class InstallService {
|
||||
private async ensureDatabase(dto: InstallRequestDto): Promise<void> {
|
||||
const db = dto.mysql.database;
|
||||
if (!/^[A-Za-z0-9_]+$/.test(db)) {
|
||||
throw new Error('数据库名仅允许字母、数字和下划线');
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, '数据库名仅允许字母、数字和下划线');
|
||||
}
|
||||
const user = encodeURIComponent(dto.mysql.username);
|
||||
const password = encodeURIComponent(dto.mysql.password);
|
||||
@@ -170,10 +187,16 @@ export class InstallService {
|
||||
await prisma.$executeRawUnsafe(
|
||||
`CREATE DATABASE IF NOT EXISTS \`${db}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`,
|
||||
);
|
||||
} catch {
|
||||
throw new Error(
|
||||
`所需库「${db}」不存在,当前账号无建库权限,请手动建库或提供足够权限的账号。` +
|
||||
`可手动执行:CREATE DATABASE \`${db}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;`,
|
||||
} catch (e) {
|
||||
const code = (e as { code?: string }).code;
|
||||
const msg = this.errorMessage(e);
|
||||
if (code === 'P1000' || /Authentication failed/i.test(msg)) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, `MySQL 连接失败:${msg}`);
|
||||
}
|
||||
throw new BusinessException(
|
||||
ErrorCode.BAD_REQUEST,
|
||||
`数据库「${db}」不存在,且当前账号无建库权限,请手动建库或提供足够权限的账号。` +
|
||||
`可手动执行:CREATE DATABASE \`${db}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;(原始错误:${msg})`,
|
||||
);
|
||||
} finally {
|
||||
await prisma.$disconnect().catch(() => undefined);
|
||||
@@ -191,6 +214,8 @@ export class InstallService {
|
||||
try {
|
||||
await redis.connect();
|
||||
await redis.ping();
|
||||
} catch (e) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, `Redis 连接失败:${this.errorMessage(e)}`);
|
||||
} finally {
|
||||
redis.disconnect();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user