🐛 fix: exception handling

This commit is contained in:
Martial BE 2024-01-13 02:46:32 +08:00 committed by Buer
parent 3b46e4ca16
commit 290523b675
25 changed files with 754 additions and 538 deletions

View File

@ -11,9 +11,10 @@ const StatusProvider = ({ children }) => {
const dispatch = useDispatch();
const loadStatus = useCallback(async () => {
let system_name = '';
try {
const res = await API.get('/api/status');
const { success, data } = res.data;
let system_name = '';
if (success) {
if (!data.chat_link) {
delete data.chat_link;
@ -45,6 +46,8 @@ const StatusProvider = ({ children }) => {
payload: data
});
}
}
} catch (error) {
showError('无法正常连接至服务器!');
}

View File

@ -13,7 +13,7 @@ API.interceptors.response.use(
if (error.response?.status === 401) {
localStorage.removeItem('user');
store.dispatch({ type: LOGIN, payload: null });
window.location.href = '/login';
// window.location.href = '/login';
}
if (error.response?.data?.message) {

View File

@ -70,6 +70,7 @@ export function showInfo(message) {
}
export async function getOAuthState() {
try {
const res = await API.get('/api/oauth/state');
const { success, message, data } = res.data;
if (success) {
@ -78,6 +79,9 @@ export async function getOAuthState() {
showError(message);
return '';
}
} catch (error) {
return '';
}
}
export async function onGitHubOAuthClicked(github_client_id, openInNewTab = false) {

View File

@ -11,6 +11,7 @@ const About = () => {
const displayAbout = async () => {
setAbout(localStorage.getItem('about') || '');
try {
const res = await API.get('/api/about');
const { success, message, data } = res.data;
if (success) {
@ -24,6 +25,10 @@ const About = () => {
showError(message);
setAbout('加载关于内容失败...');
}
} catch (error) {
setAbout('加载关于内容失败...');
}
setAboutLoaded(true);
};

View File

@ -30,6 +30,12 @@ const ForgetPasswordForm = ({ ...others }) => {
const [disableButton, setDisableButton] = useState(false);
const [countdown, setCountdown] = useState(30);
const handleFailure = (message) => {
showError(message);
setDisableButton(false);
setCountdown(30);
};
const submit = async (values, { setSubmitting }) => {
setDisableButton(true);
setSubmitting(true);
@ -38,15 +44,17 @@ const ForgetPasswordForm = ({ ...others }) => {
setSubmitting(false);
return;
}
try {
const res = await API.get(`/api/reset_password?email=${values.email}&turnstile=${turnstileToken}`);
const { success, message } = res.data;
if (success) {
showSuccess('重置邮件发送成功,请检查邮箱!');
setSendEmail(true);
} else {
showError(message);
setDisableButton(false);
setCountdown(30);
handleFailure(message);
}
} catch (error) {
handleFailure('服务器错误');
}
setSubmitting(false);
};

View File

@ -19,6 +19,7 @@ const ResetPasswordForm = () => {
const [newPassword, setNewPassword] = useState('');
const submit = async () => {
try {
const res = await API.post(`/api/user/reset`, inputs);
const { success, message } = res.data;
if (success) {
@ -29,6 +30,9 @@ const ResetPasswordForm = () => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
useEffect(() => {

View File

@ -171,6 +171,7 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => {
let res;
const modelsStr = values.models.map((model) => model.id).join(',');
values.group = values.groups.join(',');
try {
if (channelId) {
res = await API.put(`/api/channel/`, { ...values, id: parseInt(channelId), models: modelsStr });
} else {
@ -186,11 +187,18 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => {
setSubmitting(false);
setStatus({ success: true });
onOk(true);
return;
} else {
setStatus({ success: false });
showError(message);
setErrors({ submit: message });
}
} catch (error) {
setStatus({ success: false });
showError(error.message);
setErrors({ submit: error.message });
return;
}
};
function initialModel(channelModel) {
@ -213,6 +221,7 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => {
}
const loadChannel = async () => {
try {
let res = await API.get(`/api/channel/${channelId}`);
const { success, message, data } = res.data;
if (success) {
@ -237,6 +246,9 @@ const EditModal = ({ open, channelId, onCancel, onOk }) => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
useEffect(() => {

View File

@ -82,6 +82,7 @@ export default function ChannelTableRow({ item, manageChannel, handleOpenModal,
};
const updateChannelBalance = async () => {
try {
const res = await API.get(`/api/channel/update_balance/${item.id}`);
const { success, message, balance } = res.data;
if (success) {
@ -91,6 +92,9 @@ export default function ChannelTableRow({ item, manageChannel, handleOpenModal,
} else {
showError(message);
}
} catch (error) {
return;
}
};
const handleDelete = async () => {

View File

@ -36,6 +36,7 @@ export default function ChannelPage() {
const loadChannels = async (startIdx) => {
setSearching(true);
try {
const res = await API.get(`/api/channel/?p=${startIdx}`);
const { success, message, data } = res.data;
if (success) {
@ -49,6 +50,10 @@ export default function ChannelPage() {
} else {
showError(message);
}
} catch (error) {
console.error(error);
}
setSearching(false);
};
@ -70,6 +75,7 @@ export default function ChannelPage() {
return;
}
setSearching(true);
try {
const res = await API.get(`/api/channel/search?keyword=${searchKeyword}`);
const { success, message, data } = res.data;
if (success) {
@ -78,6 +84,9 @@ export default function ChannelPage() {
} else {
showError(message);
}
} catch (error) {
console.error(error);
}
setSearching(false);
};
@ -89,6 +98,8 @@ export default function ChannelPage() {
const url = '/api/channel/';
let data = { id };
let res;
try {
switch (action) {
case 'delete':
res = await API.delete(url + id);
@ -123,6 +134,9 @@ export default function ChannelPage() {
}
return res.data;
} catch (error) {
return;
}
};
// 处理刷新
@ -132,6 +146,7 @@ export default function ChannelPage() {
// 处理测试所有启用渠道
const testAllChannels = async () => {
try {
const res = await API.get(`/api/channel/test`);
const { success, message } = res.data;
if (success) {
@ -139,10 +154,14 @@ export default function ChannelPage() {
} else {
showError(message);
}
} catch (error) {
return;
}
};
// 处理删除所有禁用渠道
const deleteAllDisabledChannels = async () => {
try {
const res = await API.delete(`/api/channel/disabled`);
const { success, message, data } = res.data;
if (success) {
@ -151,11 +170,15 @@ export default function ChannelPage() {
} else {
showError(message);
}
} catch (error) {
return;
}
};
// 处理更新所有启用渠道余额
const updateAllChannelsBalance = async () => {
setSearching(true);
try {
const res = await API.get(`/api/channel/update_balance`);
const { success, message } = res.data;
if (success) {
@ -163,6 +186,10 @@ export default function ChannelPage() {
} else {
showError(message);
}
} catch (error) {
console.log(error);
}
setSearching(false);
};

View File

@ -18,6 +18,7 @@ const Dashboard = () => {
const [users, setUsers] = useState([]);
const userDashboard = async () => {
try {
const res = await API.get('/api/user/dashboard');
const { success, message, data } = res.data;
if (success) {
@ -32,9 +33,13 @@ const Dashboard = () => {
showError(message);
}
setLoading(false);
} catch (error) {
return;
}
};
const loadUser = async () => {
try {
let res = await API.get(`/api/user/self`);
const { success, message, data } = res.data;
if (success) {
@ -42,6 +47,9 @@ const Dashboard = () => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
useEffect(() => {

View File

@ -9,6 +9,7 @@ const Home = () => {
const [homePageContentLoaded, setHomePageContentLoaded] = useState(false);
const [homePageContent, setHomePageContent] = useState('');
const displayNotice = async () => {
try {
const res = await API.get('/api/notice');
const { success, message, data } = res.data;
if (success) {
@ -21,10 +22,14 @@ const Home = () => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
const displayHomePageContent = async () => {
setHomePageContent(localStorage.getItem('home_page_content') || '');
try {
const res = await API.get('/api/home_page_content');
const { success, message, data } = res.data;
if (success) {
@ -39,6 +44,9 @@ const Home = () => {
setHomePageContent('加载首页内容失败...');
}
setHomePageContentLoaded(true);
} catch (error) {
return;
}
};
useEffect(() => {

View File

@ -47,6 +47,8 @@ export default function Log() {
delete query.username;
delete query.channel;
}
try {
const res = await API.get(url, { params: query });
const { success, message, data } = res.data;
if (success) {
@ -60,6 +62,10 @@ export default function Log() {
} else {
showError(message);
}
} catch (error) {
console.log(error);
}
setSearching(false);
};

View File

@ -26,7 +26,7 @@ const validationSchema = Yup.object().shape({
email_verification_code: Yup.string().required('验证码不能为空')
});
const EmailModal = ({ open, handleClose, turnstileToken }) => {
const EmailModal = ({ open, handleClose, turnstileToken, turnstileEnabled }) => {
const theme = useTheme();
const [countdown, setCountdown] = useState(30);
const [disableButton, setDisableButton] = useState(false);
@ -36,6 +36,7 @@ const EmailModal = ({ open, handleClose, turnstileToken }) => {
const submit = async (values, { setErrors, setStatus, setSubmitting }) => {
setLoading(true);
setSubmitting(true);
try {
const res = await API.get(`/api/oauth/email/bind?email=${values.email}&code=${values.email_verification_code}`);
const { success, message } = res.data;
if (success) {
@ -47,6 +48,9 @@ const EmailModal = ({ open, handleClose, turnstileToken }) => {
showError(message);
setErrors({ submit: message });
}
} catch (error) {
console.log(error);
}
setLoading(false);
};
@ -69,7 +73,7 @@ const EmailModal = ({ open, handleClose, turnstileToken }) => {
showError('请输入邮箱');
return;
}
if (turnstileToken === '') {
if (turnstileEnabled && turnstileToken === '') {
showError('请稍后几秒重试Turnstile 正在检查用户环境!');
return;
}
@ -168,5 +172,6 @@ export default EmailModal;
EmailModal.propTypes = {
open: PropTypes.bool,
handleClose: PropTypes.func,
turnstileToken: PropTypes.string
turnstileToken: PropTypes.string,
turnstileEnabled: PropTypes.bool
};

View File

@ -59,6 +59,7 @@ export default function Profile() {
};
const loadUser = async () => {
try {
let res = await API.get(`/api/user/self`);
const { success, message, data } = res.data;
if (success) {
@ -66,6 +67,9 @@ export default function Profile() {
} else {
showError(message);
}
} catch (error) {
return;
}
};
const bindWeChat = async (code) => {
@ -84,6 +88,7 @@ export default function Profile() {
};
const generateAccessToken = async () => {
try {
const res = await API.get('/api/user/token');
const { success, message, data } = res.data;
if (success) {
@ -93,8 +98,9 @@ export default function Profile() {
} else {
showError(message);
}
console.log(turnstileEnabled, turnstileSiteKey, status);
} catch (error) {
return;
}
};
const submit = async () => {
@ -284,6 +290,7 @@ export default function Profile() {
<EmailModal
open={openEmail}
turnstileToken={turnstileToken}
turnstileEnabled={turnstileEnabled}
handleClose={() => {
setOpenEmail(false);
}}

View File

@ -46,6 +46,7 @@ const EditModal = ({ open, redemptiondId, onCancel, onOk }) => {
setSubmitting(true);
let res;
try {
if (values.is_edit) {
res = await API.put(`/api/redemption/`, { ...values, id: parseInt(redemptiondId) });
} else {
@ -72,9 +73,13 @@ const EditModal = ({ open, redemptiondId, onCancel, onOk }) => {
showError(message);
setErrors({ submit: message });
}
} catch (error) {
return;
}
};
const loadRedemptiond = async () => {
try {
let res = await API.get(`/api/redemption/${redemptiondId}`);
const { success, message, data } = res.data;
if (success) {
@ -83,6 +88,9 @@ const EditModal = ({ open, redemptiondId, onCancel, onOk }) => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
useEffect(() => {

View File

@ -30,6 +30,7 @@ export default function Redemption() {
const loadRedemptions = async (startIdx) => {
setSearching(true);
try {
const res = await API.get(`/api/redemption/?p=${startIdx}`);
const { success, message, data } = res.data;
if (success) {
@ -43,6 +44,9 @@ export default function Redemption() {
} else {
showError(message);
}
} catch (error) {
console.log(error);
}
setSearching(false);
};
@ -64,6 +68,8 @@ export default function Redemption() {
return;
}
setSearching(true);
try {
const res = await API.get(`/api/redemption/search?keyword=${searchKeyword}`);
const { success, message, data } = res.data;
if (success) {
@ -72,6 +78,10 @@ export default function Redemption() {
} else {
showError(message);
}
} catch (error) {
console.log(error);
}
setSearching(false);
};
@ -83,6 +93,8 @@ export default function Redemption() {
const url = '/api/redemption/';
let data = { id };
let res;
try {
switch (action) {
case 'delete':
res = await API.delete(url + id);
@ -105,6 +117,9 @@ export default function Redemption() {
}
return res.data;
} catch (error) {
return;
}
};
// 处理刷新

View File

@ -36,6 +36,7 @@ const OperationSetting = () => {
let [historyTimestamp, setHistoryTimestamp] = useState(now.getTime() / 1000 - 30 * 24 * 3600); // a month ago new Date().getTime() / 1000 + 3600
const getOptions = async () => {
try {
const res = await API.get('/api/option/');
const { success, message, data } = res.data;
if (success) {
@ -51,6 +52,9 @@ const OperationSetting = () => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
useEffect(() => {
@ -62,6 +66,8 @@ const OperationSetting = () => {
if (key.endsWith('Enabled')) {
value = inputs[key] === 'true' ? 'false' : 'true';
}
try {
const res = await API.put('/api/option/', {
key,
value
@ -72,6 +78,10 @@ const OperationSetting = () => {
} else {
showError(message);
}
} catch (error) {
return;
}
setLoading(false);
};
@ -146,6 +156,7 @@ const OperationSetting = () => {
};
const deleteHistoryLogs = async () => {
try {
const res = await API.delete(`/api/log/?target_timestamp=${Math.floor(historyTimestamp)}`);
const { success, message, data } = res.data;
if (success) {
@ -153,6 +164,9 @@ const OperationSetting = () => {
return;
}
showError('日志清理失败:' + message);
} catch (error) {
return;
}
};
return (

View File

@ -36,6 +36,7 @@ const OtherSetting = () => {
});
const getOptions = async () => {
try {
const res = await API.get('/api/option/');
const { success, message, data } = res.data;
if (success) {
@ -49,6 +50,9 @@ const OtherSetting = () => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
useEffect(() => {
@ -58,6 +62,7 @@ const OtherSetting = () => {
const updateOption = async (key, value) => {
setLoading(true);
try {
const res = await API.put('/api/option/', {
key,
value
@ -70,6 +75,9 @@ const OtherSetting = () => {
showError(message);
}
setLoading(false);
} catch (error) {
return;
}
};
const handleInputChange = async (event) => {
@ -106,6 +114,7 @@ const OtherSetting = () => {
};
const checkUpdate = async () => {
try {
const res = await API.get('https://api.github.com/repos/MartialBE/one-api/releases/latest');
const { tag_name, body } = res.data;
if (tag_name === process.env.REACT_APP_VERSION) {
@ -117,6 +126,9 @@ const OtherSetting = () => {
});
setShowUpdateModal(true);
}
} catch (error) {
return;
}
};
return (

View File

@ -56,6 +56,7 @@ const SystemSetting = () => {
const [showPasswordWarningModal, setShowPasswordWarningModal] = useState(false);
const getOptions = async () => {
try {
const res = await API.get('/api/option/');
const { success, message, data } = res.data;
if (success) {
@ -73,6 +74,9 @@ const SystemSetting = () => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
useEffect(() => {
@ -95,6 +99,8 @@ const SystemSetting = () => {
default:
break;
}
try {
const res = await API.put('/api/option/', {
key,
value
@ -112,6 +118,10 @@ const SystemSetting = () => {
} else {
showError(message);
}
} catch (error) {
return;
}
setLoading(false);
};

View File

@ -52,6 +52,8 @@ const EditModal = ({ open, tokenId, onCancel, onOk }) => {
values.remain_quota = parseInt(values.remain_quota);
let res;
try {
if (values.is_edit) {
res = await API.put(`/api/token/`, { ...values, id: parseInt(tokenId) });
} else {
@ -71,9 +73,13 @@ const EditModal = ({ open, tokenId, onCancel, onOk }) => {
showError(message);
setErrors({ submit: message });
}
} catch (error) {
return;
}
};
const loadToken = async () => {
try {
let res = await API.get(`/api/token/${tokenId}`);
const { success, message, data } = res.data;
if (success) {
@ -82,6 +88,9 @@ const EditModal = ({ open, tokenId, onCancel, onOk }) => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
useEffect(() => {

View File

@ -32,6 +32,7 @@ export default function Token() {
const loadTokens = async (startIdx) => {
setSearching(true);
try {
const res = await API.get(`/api/token/?p=${startIdx}`);
const { success, message, data } = res.data;
if (success) {
@ -45,6 +46,10 @@ export default function Token() {
} else {
showError(message);
}
} catch (error) {
console.log(error);
}
setSearching(false);
};
@ -75,6 +80,8 @@ export default function Token() {
return;
}
setSearching(true);
try {
const res = await API.get(`/api/token/search?keyword=${searchKeyword}`);
const { success, message, data } = res.data;
if (success) {
@ -83,6 +90,10 @@ export default function Token() {
} else {
showError(message);
}
} catch (error) {
return;
}
setSearching(false);
};
@ -94,6 +105,7 @@ export default function Token() {
const url = '/api/token/';
let data = { id };
let res;
try {
switch (action) {
case 'delete':
res = await API.delete(url + id);
@ -116,6 +128,9 @@ export default function Token() {
}
return res.data;
} catch (error) {
return;
}
};
// 处理刷新

View File

@ -16,6 +16,8 @@ const InviteCard = () => {
showSuccess(`邀请链接已复制到剪切板`);
return;
}
try {
const res = await API.get('/api/user/aff');
const { success, message, data } = res.data;
if (success) {
@ -26,6 +28,9 @@ const InviteCard = () => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
return (

View File

@ -51,6 +51,7 @@ const TopupCard = () => {
};
const getUserQuota = async () => {
try {
let res = await API.get(`/api/user/self`);
const { success, message, data } = res.data;
if (success) {
@ -58,6 +59,9 @@ const TopupCard = () => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
useEffect(() => {

View File

@ -66,6 +66,8 @@ const EditModal = ({ open, userId, onCancel, onOk }) => {
setSubmitting(true);
let res;
try {
if (values.is_edit) {
res = await API.put(`/api/user/`, { ...values, id: parseInt(userId) });
} else {
@ -85,6 +87,9 @@ const EditModal = ({ open, userId, onCancel, onOk }) => {
showError(message);
setErrors({ submit: message });
}
} catch (error) {
return;
}
};
const handleClickShowPassword = () => {
@ -96,6 +101,7 @@ const EditModal = ({ open, userId, onCancel, onOk }) => {
};
const loadUser = async () => {
try {
let res = await API.get(`/api/user/${userId}`);
const { success, message, data } = res.data;
if (success) {
@ -104,6 +110,9 @@ const EditModal = ({ open, userId, onCancel, onOk }) => {
} else {
showError(message);
}
} catch (error) {
return;
}
};
const fetchGroups = async () => {

View File

@ -30,6 +30,7 @@ export default function Users() {
const loadUsers = async (startIdx) => {
setSearching(true);
try {
const res = await API.get(`/api/user/?p=${startIdx}`);
const { success, message, data } = res.data;
if (success) {
@ -44,6 +45,10 @@ export default function Users() {
showError(message);
}
setSearching(false);
} catch (error) {
setSearching(false);
return;
}
};
const onPaginationChange = (event, activePage) => {
@ -64,6 +69,7 @@ export default function Users() {
return;
}
setSearching(true);
try {
const res = await API.get(`/api/user/search?keyword=${searchKeyword}`);
const { success, message, data } = res.data;
if (success) {
@ -73,6 +79,10 @@ export default function Users() {
showError(message);
}
setSearching(false);
} catch (error) {
setSearching(false);
return;
}
};
const handleSearchKeyword = (event) => {
@ -95,6 +105,7 @@ export default function Users() {
break;
}
try {
res = await API.post(url, data);
const { success, message } = res.data;
if (success) {
@ -105,6 +116,9 @@ export default function Users() {
}
return res.data;
} catch (error) {
return;
}
};
// 处理刷新