fix(auth): 为API请求添加认证错误处理
在API请求失败时,区分认证错误和其他错误类型 修改错误处理逻辑以显示不同的错误消息
This commit is contained in:
parent
2f1f166fb8
commit
e92fbfe2c3
12
web/main.js
12
web/main.js
|
|
@ -505,14 +505,18 @@ const app = createApp({
|
||||||
/**
|
/**
|
||||||
* 加载模板列表
|
* 加载模板列表
|
||||||
*/
|
*/
|
||||||
const loadTemplates = async () => {
|
const loadTemplates = async () => {
|
||||||
try {
|
try {
|
||||||
state.templates = await Api.fetchTemplates();
|
state.templates = await Api.fetchTemplates();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error.isAuthError) {
|
||||||
|
showMessage('请先登录', 'error');
|
||||||
|
} else {
|
||||||
showMessage('加载模板失败', 'error');
|
showMessage('加载模板失败', 'error');
|
||||||
|
}
|
||||||
state.templates = [];
|
state.templates = [];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建模板
|
* 创建模板
|
||||||
|
|
@ -993,8 +997,12 @@ const app = createApp({
|
||||||
state.sqlText = '';
|
state.sqlText = '';
|
||||||
state.sqlExplainDesc = '';
|
state.sqlExplainDesc = '';
|
||||||
state.sqlVisible = false;
|
state.sqlVisible = false;
|
||||||
|
if (error.isAuthError) {
|
||||||
|
showMessage('请先登录', 'error');
|
||||||
|
} else {
|
||||||
showMessage('加载SQL失败', 'error');
|
showMessage('加载SQL失败', 'error');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// ==================== 对话框尺寸管理 ====================
|
// ==================== 对话框尺寸管理 ====================
|
||||||
|
|
|
||||||
|
|
@ -153,7 +153,9 @@ const get = async (endpoint, options = {}) => {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const data = await response.json().catch(() => ({}));
|
const data = await response.json().catch(() => ({}));
|
||||||
if (response.status === 401 && data.message) {
|
if (response.status === 401 && data.message) {
|
||||||
throw new Error(data.message);
|
const authError = new Error(data.message);
|
||||||
|
authError.isAuthError = true;
|
||||||
|
throw authError;
|
||||||
}
|
}
|
||||||
throw new Error(`请求失败: ${response.status} ${response.statusText}`);
|
throw new Error(`请求失败: ${response.status} ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
@ -185,7 +187,9 @@ const post = async (endpoint, body, options = {}) => {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const data = await response.json().catch(() => ({}));
|
const data = await response.json().catch(() => ({}));
|
||||||
if (response.status === 401 && data.message) {
|
if (response.status === 401 && data.message) {
|
||||||
throw new Error(data.message);
|
const authError = new Error(data.message);
|
||||||
|
authError.isAuthError = true;
|
||||||
|
throw authError;
|
||||||
}
|
}
|
||||||
const errorText = await response.text();
|
const errorText = await response.text();
|
||||||
throw new Error(errorText || `请求失败: ${response.status}`);
|
throw new Error(errorText || `请求失败: ${response.status}`);
|
||||||
|
|
@ -221,7 +225,9 @@ const patch = async (endpoint, body) => {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const data = await response.json().catch(() => ({}));
|
const data = await response.json().catch(() => ({}));
|
||||||
if (response.status === 401 && data.message) {
|
if (response.status === 401 && data.message) {
|
||||||
throw new Error(data.message);
|
const authError = new Error(data.message);
|
||||||
|
authError.isAuthError = true;
|
||||||
|
throw authError;
|
||||||
}
|
}
|
||||||
const errorText = await response.text();
|
const errorText = await response.text();
|
||||||
throw new Error(errorText || `请求失败: ${response.status}`);
|
throw new Error(errorText || `请求失败: ${response.status}`);
|
||||||
|
|
@ -250,7 +256,9 @@ const del = async (endpoint, options = {}) => {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const data = await response.json().catch(() => ({}));
|
const data = await response.json().catch(() => ({}));
|
||||||
if (response.status === 401 && data.message) {
|
if (response.status === 401 && data.message) {
|
||||||
throw new Error(data.message);
|
const authError = new Error(data.message);
|
||||||
|
authError.isAuthError = true;
|
||||||
|
throw authError;
|
||||||
}
|
}
|
||||||
const errorText = await response.text();
|
const errorText = await response.text();
|
||||||
throw new Error(errorText || `请求失败: ${response.status}`);
|
throw new Error(errorText || `请求失败: ${response.status}`);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue