Fix: api request error flow

This commit is contained in:
unitwk 2023-09-02 13:56:02 +08:00
parent 6bbb5faa8d
commit fc3126f141
2 changed files with 22 additions and 18 deletions

View File

@ -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);
}
});
};

View File

@ -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)));
}
}
}