fix(auth): 统一未登录错误提示并优化前端错误处理
修改后端中间件返回的未登录错误提示信息为统一格式 前端API请求添加对401错误的特殊处理,显示服务端返回的具体错误信息
This commit is contained in:
parent
c82e3d7d7c
commit
2f1f166fb8
|
|
@ -100,7 +100,7 @@ func withAuth(apiDomain string) func(http.Handler) http.Handler {
|
||||||
// 获取 token
|
// 获取 token
|
||||||
token := r.Header.Get("token")
|
token := r.Header.Get("token")
|
||||||
if token == "" {
|
if token == "" {
|
||||||
http.Error(w, "{\"code\":401,\"message\":\"未提供认证token\",\"data\":null}", http.StatusUnauthorized)
|
http.Error(w, "{\"code\":401,\"message\":\"请先登录\",\"data\":null}", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,7 +108,7 @@ func withAuth(apiDomain string) func(http.Handler) http.Handler {
|
||||||
authURL := fmt.Sprintf("%s/auth/admin/dataPermission", apiDomain)
|
authURL := fmt.Sprintf("%s/auth/admin/dataPermission", apiDomain)
|
||||||
req, err := http.NewRequest("GET", authURL, nil)
|
req, err := http.NewRequest("GET", authURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "{\"code\":500,\"message\":\"创建认证请求失败\",\"data\":null}", http.StatusInternalServerError)
|
http.Error(w, "{\"code\":401,\"message\":\"请先登录\",\"data\":null}", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -151,6 +151,10 @@ const get = async (endpoint, options = {}) => {
|
||||||
headers: buildHeaders()
|
headers: buildHeaders()
|
||||||
});
|
});
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
const data = await response.json().catch(() => ({}));
|
||||||
|
if (response.status === 401 && data.message) {
|
||||||
|
throw new Error(data.message);
|
||||||
|
}
|
||||||
throw new Error(`请求失败: ${response.status} ${response.statusText}`);
|
throw new Error(`请求失败: ${response.status} ${response.statusText}`);
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.json();
|
||||||
|
|
@ -179,6 +183,10 @@ const post = async (endpoint, body, options = {}) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
const data = await response.json().catch(() => ({}));
|
||||||
|
if (response.status === 401 && data.message) {
|
||||||
|
throw new Error(data.message);
|
||||||
|
}
|
||||||
const errorText = await response.text();
|
const errorText = await response.text();
|
||||||
throw new Error(errorText || `请求失败: ${response.status}`);
|
throw new Error(errorText || `请求失败: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
@ -211,6 +219,10 @@ const patch = async (endpoint, body) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
const data = await response.json().catch(() => ({}));
|
||||||
|
if (response.status === 401 && data.message) {
|
||||||
|
throw new Error(data.message);
|
||||||
|
}
|
||||||
const errorText = await response.text();
|
const errorText = await response.text();
|
||||||
throw new Error(errorText || `请求失败: ${response.status}`);
|
throw new Error(errorText || `请求失败: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
@ -236,6 +248,10 @@ const del = async (endpoint, options = {}) => {
|
||||||
headers: buildHeaders()
|
headers: buildHeaders()
|
||||||
});
|
});
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
const data = await response.json().catch(() => ({}));
|
||||||
|
if (response.status === 401 && data.message) {
|
||||||
|
throw new Error(data.message);
|
||||||
|
}
|
||||||
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