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

Merged
merged 6 commits into from
Oct 4, 2024

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 15a52f2
Latest job logs Run #11177902310
BadgeDownload
BadgeDownload

src/popup/components/ConnectWalletForm.tsx Outdated Show resolved Hide resolved
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;

Copy link
Member Author

Choose a reason for hiding this comment

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

If we allow passing { message: string } (which matches Error), we've to always add a check for isErrorWithKey, which is more expensive then typeof err === 'string'.

I'll look into rationalizing error types as a follow-up. I basically want only Error ({ messsage: string }) and ErrorWithKeyLike, with failure or ErrorResponse allowing returning serialization of either.

src/popup/components/ConnectWalletForm.tsx Show resolved Hide resolved
src/popup/components/ConnectWalletForm.tsx Outdated Show resolved Hide resolved
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.

@sidvishnoi sidvishnoi merged commit 3a292ae into main Oct 4, 2024
9 checks passed
@sidvishnoi sidvishnoi deleted the preserve-error-key branch October 4, 2024 11:54
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