chore: import upstream snapshot with attribution
Update draft releases / main (push) Has been cancelled
Build and push docs image / build-image (push) Has been cancelled
Build Web Application / build-web (macos-latest) (push) Has been cancelled
Build Web Application / build-web (ubuntu-latest) (push) Has been cancelled
Python Code Quality Checks / build (push) Has been cancelled
Test Python / test-python (macos-latest, 3.10) (push) Has been cancelled
Test Python / test-python (macos-latest, 3.11) (push) Has been cancelled
Test Python / test-python (ubuntu-latest, 3.10) (push) Has been cancelled
Test Python / test-python (ubuntu-latest, 3.11) (push) Has been cancelled
Update draft releases / main (push) Has been cancelled
Build and push docs image / build-image (push) Has been cancelled
Build Web Application / build-web (macos-latest) (push) Has been cancelled
Build Web Application / build-web (ubuntu-latest) (push) Has been cancelled
Python Code Quality Checks / build (push) Has been cancelled
Test Python / test-python (macos-latest, 3.10) (push) Has been cancelled
Test Python / test-python (macos-latest, 3.11) (push) Has been cancelled
Test Python / test-python (ubuntu-latest, 3.10) (push) Has been cancelled
Test Python / test-python (ubuntu-latest, 3.11) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export * from './interceptors';
|
||||
@@ -0,0 +1,50 @@
|
||||
import { notification } from 'antd';
|
||||
import { AxiosError } from 'axios';
|
||||
import { ApiResponse, FailedTuple, ResponseType, SuccessTuple } from '../';
|
||||
|
||||
/**
|
||||
* Response processing
|
||||
*
|
||||
* @param promise request
|
||||
* @param ignoreCodes ignore error codes
|
||||
* @returns
|
||||
*/
|
||||
export const apiInterceptors = <T = any, D = any>(
|
||||
promise: Promise<ApiResponse<T, D>>,
|
||||
ignoreCodes?: '*' | (number | string)[],
|
||||
) => {
|
||||
return promise
|
||||
.then<SuccessTuple<T, D>>(response => {
|
||||
const { data } = response;
|
||||
if (!data) {
|
||||
throw new Error('Network Error!');
|
||||
}
|
||||
if (!data.success) {
|
||||
if (ignoreCodes === '*' || (data.err_code && ignoreCodes && ignoreCodes.includes(data.err_code))) {
|
||||
return [null, data.data, data, response];
|
||||
} else {
|
||||
notification.error({
|
||||
message: `Request error`,
|
||||
description: data?.err_msg ?? 'The interface is abnormal. Please try again later',
|
||||
});
|
||||
}
|
||||
}
|
||||
return [null, data.data, data, response];
|
||||
})
|
||||
.catch<FailedTuple<T, D>>((err: Error | AxiosError<T, D>) => {
|
||||
let errMessage = err.message;
|
||||
if (err instanceof AxiosError) {
|
||||
try {
|
||||
const { err_msg } = JSON.parse(err.request.response) as ResponseType<null>;
|
||||
err_msg && (errMessage = err_msg);
|
||||
} catch {
|
||||
/* empty */
|
||||
}
|
||||
}
|
||||
notification.error({
|
||||
message: `Request error`,
|
||||
description: errMessage,
|
||||
});
|
||||
return [err, null, null, null];
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user