mirror of
https://github.com/MCSManager/MCSManager.git
synced 2025-04-06 17:10:29 +08:00
Fix: api request error flow
This commit is contained in:
parent
6bbb5faa8d
commit
fc3126f141
@ -7,7 +7,6 @@ import { setUserApiKey, updatePassword } from "@/services/apis/user";
|
||||
import { message } from "ant-design-vue";
|
||||
import type { FormInstance } from "ant-design-vue";
|
||||
import CopyButton from "@/components/CopyButton.vue";
|
||||
import { sleep } from "@/tools/commom";
|
||||
const { state, updateUserInfo } = useAppStateStore();
|
||||
const { state: tools } = useAppToolsStore();
|
||||
|
||||
@ -23,15 +22,19 @@ const formState = reactive({
|
||||
const formRef = ref<FormInstance>();
|
||||
|
||||
const handleGenerateApiKey = async (enable: boolean) => {
|
||||
const res = await execute({
|
||||
data: {
|
||||
enable
|
||||
try {
|
||||
const res = await execute({
|
||||
data: {
|
||||
enable
|
||||
},
|
||||
forceRequest: true
|
||||
});
|
||||
if (res.value) {
|
||||
updateUserInfo();
|
||||
return message.success(t("更新成功"));
|
||||
}
|
||||
});
|
||||
|
||||
if (res.value) {
|
||||
updateUserInfo();
|
||||
return message.success(t("更新成功"));
|
||||
} catch (error: any) {
|
||||
return message.error(error.message);
|
||||
}
|
||||
};
|
||||
|
||||
@ -50,7 +53,7 @@ const handleChangePassword = async () => {
|
||||
updateUserInfo();
|
||||
return message.success(t("更新成功"));
|
||||
} catch (error: any) {
|
||||
return message.error(error.response.data.data);
|
||||
return message.error(error.message);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -60,11 +60,11 @@ class ApiService {
|
||||
});
|
||||
}
|
||||
|
||||
private async sendRequest(reqId: string, config: AxiosRequestConfig) {
|
||||
private async sendRequest(reqId: string, config: RequestConfig) {
|
||||
try {
|
||||
const startTime = Date.now();
|
||||
|
||||
if (this.responseMap.has(reqId)) {
|
||||
if (this.responseMap.has(reqId) && !config.forceRequest) {
|
||||
const cache = this.responseMap.get(reqId) as ResponseDataRecord;
|
||||
if (cache.timestamp + this.RESPONSE_CACHE_TIME > Date.now()) {
|
||||
return this.event.emit(reqId, cache.data);
|
||||
@ -94,14 +94,15 @@ class ApiService {
|
||||
if (axiosErr?.response?.data) {
|
||||
const protocol = axiosErr?.response?.data as IPanelResponseProtocol;
|
||||
if (protocol.data && protocol.status !== 200) {
|
||||
throw new Error(String(protocol.data));
|
||||
} else {
|
||||
throw error;
|
||||
this.event.emit(reqId, new Error(String(protocol.data)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new Error(otherErr?.message ? otherErr?.message : String(otherErr));
|
||||
} finally {
|
||||
this.event.emit(reqId, undefined);
|
||||
if (otherErr instanceof Error) {
|
||||
this.event.emit(reqId, otherErr);
|
||||
} else {
|
||||
this.event.emit(reqId, new Error(String(otherErr)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user