fix: 安装页测试连接失败时向前端返回真实错误原因
This commit is contained in:
@@ -271,6 +271,7 @@
|
||||
- 复用约定:列表用 `composables/useTablePage.ts` + `page table-page` + `n-data-table.table-fill flex-height`(关内置分页、外置 `n-pagination`,`pageSize=0` 不分页);上传用 `MediaUpload.vue`(单/少量)与 `MediaSorter.vue`(多图排序 captionable);`TraceCodePanel.vue` 复用于全局与档案内追溯码管理;`openMediaViewer.ts`=PhotoSwipe(带工具条);`utils/mediaUrl.ts` 规整媒体 URL。
|
||||
- 主要页面 `views/`:Dashboard、Templates、Artworks/ArtworkEdit/ArtworkTraceCodes、TraceCodes、Outbound、Audit、QueryLogs、OperationLogs、Users、Settings、Profile、Login、Install(三步向导:基础→数据库→存储)。
|
||||
- 安装页 `baseUrlHint`(中英):三行换行说明对外访问地址(域名 / IP:端口 / 本地默认);`.hint` 使用 `white-space: pre-line` 渲染 `\n`。
|
||||
- 安装页错误信息穿透(2026-07-09):`InstallService.testConnection/testStorage/install` 内部原先 `throw new Error(...)`,会被全局 `AllExceptionsFilter` 当作未知异常统一替换为「服务器内部错误」,导致安装页测试连接失败时前端只能看到通用报错、看不到真实原因(需翻服务端日志才能看到 `MySQL 连接失败:Authentication failed...`)。改为 `throw new BusinessException(ErrorCode.BAD_REQUEST, message)`,令真实 message 透传到响应体,前端 `Install.vue` 的 `post()` 已读取 `body.message` 直接展示;同时补全 Redis 测试连接的 try/catch 包装(原来无 catch,原始 ioredis 报错会以未包装 Error 形式再次被过滤器吞掉)、`assertMysql/assertRedis`(正式安装提交时用)与 `ensureDatabase`(无建库权限)均补充清晰中文前缀("MySQL 连接失败:""Redis 连接失败:""数据库「x」不存在,且当前账号无建库权限...")。这些错误文案延续既有约定为纯中文动态拼接(底层驱动报错本身多为英文技术细节,不做逐句 i18n,与既有 install 静态文案的双语约定不冲突)。已用直连接口验证:错误密码返回 400+真实 Prisma 报错、错误端口 Redis 返回 400+真实 ioredis 报错。
|
||||
|
||||
### apps/client(Vue3+Vite 移动优先溯源页)
|
||||
- 入口 `views/Trace.vue`(顶部媒体无缝轮播、防伪横幅、动态字段、时间轴节点媒体默认收缩、水印证书、区块链卡、i18n、暗色变量)+ `Home.vue`;`composables/openMediaViewer.ts`(PhotoSwipe,已弃用旧 MediaViewer.vue);hash 路由 `/trace/:code`,短链 `/t/{code}` 由 API 302。
|
||||
|
||||
@@ -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