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