import { Path, UseFormSetError } from 'react-hook-form'; import { ApiValidationError } from '..'; export const useOnSubmitCatchError = >( callback: (values: FieldValues) => Promise, setError: UseFormSetError, ) => async (values: FieldValues) => { try { await callback(values); } catch (e) { if (e instanceof ApiValidationError) { Object.entries(e.data).forEach(([key, value]) => { for (const error of value) { setError(key as Path, error); } }); } else if (e instanceof Error) { setError('root', { message: e.message }); } else { setError('root', { message: 'Unknown error' }); } } };