Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(console): catch timeout error when submitting form #6431

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/console/src/utils/form.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { HTTPError } from 'ky';
import { HTTPError, TimeoutError } from 'ky';
import { type FieldValues, type SubmitHandler } from 'react-hook-form';
import { toast } from 'react-hot-toast';

/**
* After upgrading the react-hook-form to v7.42.0, the `isSubmitting` flag does not recover when the submit handler throws.
* So we need to catch the error and do nothing if the error is an HTTPError and the status code is not 401 to prevent the `isSubmitting` flag from being stuck.
* So we need to catch the error and do nothing if the error is one of the following to prevent the `isSubmitting` flag from being stuck:
* - HTTPError with a status code that is not 401
* - TimeoutError
*
* Reference: https://github.com/orgs/react-hook-form/discussions/10103#discussioncomment-5927542
*/
export const trySubmitSafe =
Expand All @@ -13,6 +17,13 @@ export const trySubmitSafe =
await handler(formData, event);
} catch (error) {
if (error instanceof HTTPError && error.response.status !== 401) {
// Returned directly, since the error has been handled by the `use-api` hook.
return;
}

if (error instanceof TimeoutError) {
// Display a toast message for the timeout error.
toast.error(error.message);
return;
}

Expand Down
Loading