feat: error cache (#2145)

This commit is contained in:
chenos 2023-06-28 15:29:01 +08:00 committed by GitHub
parent 4fc64af173
commit 15f824387e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,6 +14,8 @@ const handleErrorMessage = (error) => {
});
};
};
const errorCache = new Map();
export class APIClient extends APIClientSDK {
services: Record<string, Result<any, any>> = {};
silence = false;
@ -49,9 +51,24 @@ export class APIClient extends APIClientSDK {
if (error?.response?.data?.type === 'application/json') {
handleErrorMessage(error);
} else {
if (errorCache.size > 10) {
errorCache.clear();
}
let errs = error?.response?.data?.errors || [{ message: 'Server error' }];
errs = errs.filter((error) => {
const lastTime = errorCache.get(error.message);
if (lastTime && new Date().getTime() - lastTime < 500) {
return false;
}
errorCache.set(error.message, new Date().getTime());
return true;
});
if (errs.length === 0) {
throw error;
}
notification.error({
message: error?.response?.data?.errors?.map?.((error: any) => {
return React.createElement('div', { children: error.message });
message: errs?.map?.((error: any) => {
return React.createElement('div', {}, error.message);
}),
});
}