0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor: catch withtyped errors

This commit is contained in:
Gao Sun 2024-09-18 22:13:38 +08:00
parent eee4317dac
commit 3c9b1eaafe
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
2 changed files with 17 additions and 10 deletions

View file

@ -19,6 +19,7 @@ import RadioGroup, { Radio } from '@/ds-components/RadioGroup';
import TextInput from '@/ds-components/TextInput'; import TextInput from '@/ds-components/TextInput';
import useTheme from '@/hooks/use-theme'; import useTheme from '@/hooks/use-theme';
import modalStyles from '@/scss/modal.module.scss'; import modalStyles from '@/scss/modal.module.scss';
import { trySubmitSafe } from '@/utils/form';
import EnvTagOptionContent from './EnvTagOptionContent'; import EnvTagOptionContent from './EnvTagOptionContent';
import SelectTenantPlanModal from './SelectTenantPlanModal'; import SelectTenantPlanModal from './SelectTenantPlanModal';
@ -57,16 +58,18 @@ function CreateTenantModal({ isOpen, onClose }: Props) {
}; };
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
const onCreateClick = handleSubmit(async (data: CreateTenantData) => { const onCreateClick = handleSubmit(
const { tag } = data; trySubmitSafe(async (data: CreateTenantData) => {
if (tag === TenantTag.Development) { const { tag } = data;
await createTenant(data); if (tag === TenantTag.Development) {
toast.success(t('tenants.create_modal.tenant_created')); await createTenant(data);
return; toast.success(t('tenants.create_modal.tenant_created'));
} return;
}
setTenantData(data); setTenantData(data);
}); })
);
return ( return (
<Modal <Modal

View file

@ -1,3 +1,4 @@
import { ResponseError } from '@withtyped/client';
import { HTTPError, TimeoutError } from 'ky'; import { HTTPError, TimeoutError } from 'ky';
import { type FieldValues, type SubmitHandler } from 'react-hook-form'; import { type FieldValues, type SubmitHandler } from 'react-hook-form';
import { toast } from 'react-hot-toast'; import { toast } from 'react-hot-toast';
@ -16,7 +17,10 @@ export const trySubmitSafe =
try { try {
await handler(formData, event); await handler(formData, event);
} catch (error) { } catch (error) {
if (error instanceof HTTPError && error.response.status !== 401) { if (
(error instanceof HTTPError || error instanceof ResponseError) &&
error.response.status !== 401
) {
// Returned directly, since the error has been handled by the `use-api` hook. // Returned directly, since the error has been handled by the `use-api` hook.
return; return;
} }