This commit is contained in:
@@ -19,6 +19,31 @@ import type { AxiosRequestConfigRetry, RequestOptions, Result } from '@/types/ax
|
||||
import { AxiosCanceler } from './AxiosCancel';
|
||||
import type { CreateAxiosOptions } from './AxiosTransform';
|
||||
|
||||
function localizeApiError(code: string | undefined, detail: string | undefined, status?: number): string {
|
||||
const messages: Record<string, string> = {
|
||||
DATABASE_CONNECTION_FAILED: '数据库连接失败,请检查地址、端口、库名和账号权限',
|
||||
DATABASE_INITIALIZATION_FAILED: '数据库初始化失败,请确认使用专用空库并检查迁移权限',
|
||||
DATABASE_ENGINE_NOT_SUPPORTED: '当前版本仅支持 MySQL 8.4',
|
||||
MYSQL_VERSION_UNSUPPORTED: 'MySQL 版本不受支持,需要 MySQL 8.4.x',
|
||||
DATABASE_COLLATION_PREPARATION_FAILED: '数据库字符集或排序规则准备失败',
|
||||
DATABASE_MIGRATION_STATE_INVALID: '数据库迁移历史无效,请使用空库或先修复迁移状态',
|
||||
DATABASE_SCHEMA_INSPECTION_FAILED: '数据库结构检查失败',
|
||||
SETUP_CODE_INVALID: '安装码不正确',
|
||||
SETUP_LOCKED: '安装向导已锁定',
|
||||
SETUP_STATE_WRITE_FAILED: '安装状态文件写入失败,请检查应用目录权限',
|
||||
ADMIN_PASSWORD_MISMATCH: '管理员密码确认不一致',
|
||||
};
|
||||
if (code && messages[code]) return messages[code];
|
||||
if (detail && /[\u4E00-\u9FFF]/.test(detail)) return detail;
|
||||
if (status === 401) return '登录会话已失效';
|
||||
if (status === 403) return '当前账号没有执行此操作的权限';
|
||||
if (status === 404) return '请求的资源不存在';
|
||||
if (status === 409) return '数据已发生变化,请刷新后重试';
|
||||
if (status === 422) return '提交的数据未通过校验';
|
||||
if (status && status >= 500) return '服务器处理失败,请查看服务日志';
|
||||
return '网络请求失败,请检查网络连接后重试';
|
||||
}
|
||||
|
||||
/**
|
||||
* Axios 模块
|
||||
*/
|
||||
@@ -318,7 +343,7 @@ export class VAxios {
|
||||
const problem = e.response?.data as
|
||||
{ code?: string; detail?: string; requestId?: string; fieldErrors?: Record<string, string> } | undefined;
|
||||
const requestId = problem?.requestId || e.response?.headers?.['x-request-id'];
|
||||
const message = problem?.detail || (e.response?.status === 401 ? '登录会话已失效' : '网络请求失败');
|
||||
const message = localizeApiError(problem?.code, problem?.detail, e.response?.status);
|
||||
const error = new Error(requestId ? `${message}(请求编号:${requestId})` : message);
|
||||
Object.assign(error, {
|
||||
code: problem?.code,
|
||||
|
||||
Reference in New Issue
Block a user