mirror of
https://github.com/logto-io/logto.git
synced 2025-03-31 22:51:25 -05:00
fix(ui): catch request exceptions with no response body (#790)
catch request exceptions with no response body
This commit is contained in:
parent
1255c6d1e7
commit
48de9c072b
3 changed files with 26 additions and 14 deletions
|
@ -81,8 +81,8 @@ const translation = {
|
|||
passwords_do_not_match: 'Passwords do not match.',
|
||||
agree_terms_required: 'You must agree to the Terms of Use before continuing.',
|
||||
invalid_passcode: 'The passcode is invalid.',
|
||||
request: 'Request error ({{code}}):{{message}}',
|
||||
unknown: 'Unknown error, please try again later.',
|
||||
request: 'Request Error:{{message}}',
|
||||
unknown: 'Request Error, please try again later.',
|
||||
},
|
||||
},
|
||||
admin_console: {
|
||||
|
|
|
@ -81,8 +81,8 @@ const translation = {
|
|||
passwords_do_not_match: '密码不匹配。',
|
||||
agree_terms_required: '你需要同意使用条款以继续。',
|
||||
invalid_passcode: '无效的验证码。',
|
||||
request: '请求错误({{ code }}):{{ message }}',
|
||||
unknown: '未知错误,请稍后重试。',
|
||||
request: '请求异常:{{ message }}',
|
||||
unknown: '请求异常,请稍后重试。',
|
||||
},
|
||||
},
|
||||
admin_console: {
|
||||
|
|
|
@ -29,6 +29,26 @@ function useApi<Args extends any[], Response>(
|
|||
|
||||
const { setLoading, setToast } = useContext(PageContext);
|
||||
|
||||
const parseError = useCallback(
|
||||
async (error: unknown) => {
|
||||
if (error instanceof HTTPError) {
|
||||
try {
|
||||
const kyError = await error.response.json<RequestErrorBody>();
|
||||
setError(kyError);
|
||||
} catch {
|
||||
setToast(t('error.unknown'));
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
setToast(t('error.unknown'));
|
||||
console.log(error);
|
||||
},
|
||||
[setToast, t]
|
||||
);
|
||||
|
||||
const run = useCallback(
|
||||
async (...args: Args) => {
|
||||
setLoading(true);
|
||||
|
@ -41,20 +61,12 @@ function useApi<Args extends any[], Response>(
|
|||
|
||||
return result;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof HTTPError && error.response.body) {
|
||||
const kyError = await error.response.json<RequestErrorBody>();
|
||||
setError(kyError);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
setToast(t('error.unknown'));
|
||||
console.log(error);
|
||||
void parseError(error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
},
|
||||
[api, setLoading, setToast, t]
|
||||
[api, parseError, setLoading]
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
|
|
Loading…
Add table
Reference in a new issue