1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-12-22 09:13:35 +01:00

error handling before send

This commit is contained in:
Matthias Kretschmann 2023-11-05 15:54:17 +00:00
parent ad22b84349
commit 324d032326
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 31 additions and 16 deletions

View File

@ -19,7 +19,10 @@ export function Web3Form(): ReactElement {
const [error, setError] = useState<string>()
useEffect(() => {
if (!amount || amount === '' || !selectedToken?.balance) return
if (!amount || amount === '' || !selectedToken?.balance) {
setError(undefined)
return
}
if (Number(amount) > Number(selectedToken?.balance)) {
setError('Exceeds balance')

View File

@ -103,13 +103,13 @@
text-shadow: none;
}
.error {
.errorOutput {
color: red;
font-size: var(--font-size-small);
font-size: var(--font-size-mini);
margin-left: var(--tokenWidth);
text-align: left;
position: absolute;
left: 0;
bottom: 0;
right: calc(var(--spacer) / 3);
bottom: -3px;
width: 100%;
text-align: right;
}

View File

@ -57,7 +57,7 @@ export function InputGroup({
Preview
</button>
{error ? <span className={styles.error}>{error}</span> : null}
{error ? <span className={styles.errorOutput}>{error}</span> : null}
</div>
<Conversion />

View File

@ -4,16 +4,18 @@
word-break: break-all;
background: none;
padding: 0;
}
code.to,
code.from {
font-size: var(--font-size-mini);
}
.to:last-child:not(:only-child),
.from:last-child:not(:only-child) {
.to:last-child {
padding-left: 14px;
}
.to:last-child:not(:only-child)::first-letter,
.from:last-child:not(:only-child)::first-letter {
.to:last-child::first-letter {
margin-left: -14px;
}

View File

@ -44,8 +44,7 @@ export function Data({
<td className={styles.label}>You are</td>
{ensFrom ? (
<td title={`${ensFrom} successfully resolved to ${from}`}>
<code className={styles.from}>{ensFrom}</code>
<code className={styles.from}>{`${from}`}</code>
<span className={styles.from}>{ensFrom}</span>
</td>
) : (
<td>
@ -73,7 +72,7 @@ export function Data({
<tr>
<td className={styles.label}>to</td>
<td title={`${ensResolved} successfully resolved to ${to}`}>
<code className={styles.to}>{ensResolved}</code>
<span className={styles.to}>{ensResolved}</span>
<code className={styles.to}>{`${to}`}</code>
</td>
</tr>

View File

@ -26,10 +26,21 @@ export function useSend({
const result = await send(selectedToken, txConfig)
$txHash.set(result?.hash)
} catch (error: unknown) {
console.error((error as Error).message)
const errorMessage = (error as Error).message
console.error(errorMessage)
// only expose useful errors in UI
if ((error as Error).message.includes('User rejected the request.')) {
const terribleErrorMessages = [
'User rejected the request.',
'User denied transaction signature.',
'Cannot read properties of undefined'
]
if (
terribleErrorMessages.some((terribleMessage) =>
errorMessage.includes(terribleMessage)
)
) {
setError(undefined)
} else {
setError((error as Error).message)