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

View file

@ -1,3 +1,4 @@
import { ResponseError } from '@withtyped/client';
import { HTTPError, TimeoutError } from 'ky';
import { type FieldValues, type SubmitHandler } from 'react-hook-form';
import { toast } from 'react-hot-toast';
@ -16,7 +17,10 @@ export const trySubmitSafe =
try {
await handler(formData, event);
} 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.
return;
}