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

refactor(ConnectWalletForm): preserve key in errors #641

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

sidvishnoi
Copy link
Member

@sidvishnoi sidvishnoi commented Oct 3, 2024

Earlier, we stored only localized message in errors[foo]. Now we preserve the key and original error under {info}; and add the localized message under {message}.

This will help with error handling based on key, and paves way to support retry add-key on retry-able errors (such as timed out waiting for login, wrong account was logged in, key-add consent accidentally declined etc.)

Part of #613

@github-actions github-actions bot added the area: popup Improvements or additions to extension popup label Oct 3, 2024
Copy link

github-actions bot commented Oct 3, 2024

Extension builds preview

Name Link
Latest commit a012ce4
Latest job logs Run #11162298227
BadgeDownload
BadgeDownload

const [autoKeyShareFailed, setAutoKeyShareFailed] = React.useState(
isAutoKeyAddFailed(state),
);

const resetState = React.useCallback(async () => {
await clearConnectState();
setErrors((_) => ({ ..._, keyPair: '', connect: '' }));
setErrors((_) => ({ ..._, keyPair: null, connect: null }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest renaming the parameter to prev or something else that suggests that this is the previous state. Clearer than _.

Suggested change
setErrors((_) => ({ ..._, keyPair: null, connect: null }));
setErrors((prev) => ({ ...prev, keyPair: null, connect: null }));

setAutoKeyShareFailed(false);
}, [clearConnectState]);

const toErrorInfo = React.useCallback(
(err?: string | ErrorWithKeyLike | null): ErrorInfo | null => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have the error as string as well? Can't we pass the whole error directly? For example here:

try {
setIsValidating((_) => ({ ..._, walletAddressUrl: true }));
const url = new URL(toWalletAddressUrl(walletAddressUrl));
const walletAddress = await getWalletInfo(url.toString());
setWalletAddressInfo(walletAddress);
} catch (error) {
setErrors((_) => ({
..._,
walletAddressUrl: toErrorInfo(error.message),
}));
return false;

Comment on lines +94 to +95
keyPair: state?.status === 'error:key' ? toErrorInfo(state.error) : null,
connect: state?.status === 'error' ? toErrorInfo(state.error) : null,
Copy link
Member

@raducristianpopa raducristianpopa Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my information, state represents the transient state?

Comment on lines +165 to +166
const handleSubmit = async (ev?: React.FormEvent<HTMLFormElement>) => {
ev?.preventDefault();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the event optional now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because now we manually trigger the submit (without the form also), like on "Accept consent" or "retry".

Comment on lines +173 to +174
walletAddressUrl: toErrorInfo(errWalletAddressUrl),
amount: toErrorInfo(errAmount),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are mixing errors as exceptions and errors as value with the validateWalletAddressUrl and validateAmount. Not a big fan, but I understand why this approach.

It might be worth looking into neverthrow later on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: popup Improvements or additions to extension popup
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants