Skip to content

Commit 46f2639

Browse files
authored
Merge pull request #302 from oasisprotocol/lw/wrap-short-error
Show shorter error messages (e.g. if user rejects a tx)
2 parents 1e61246 + bedda92 commit 46f2639

4 files changed

Lines changed: 17 additions & 14 deletions

File tree

.changelog/302.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Show shorter error messages (e.g. if user rejects a tx)

move/src/withdraw/Withdraw.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { amountPattern, consensusConfig, multiplyConsensusToSapphire } from '../
2323
import { withdrawFeeAmount, minimalWithdrawableAmount } from './withdrawToConsensus.ts'
2424
import { ExistingBalance } from './ExistingBalance.tsx'
2525
import { ConsensusAccount, SapphireAccount } from './useGenerateSapphireAccount.ts'
26+
import { BaseError } from 'wagmi'
2627

2728
interface FormItem<T = string> {
2829
value: T | undefined
@@ -205,7 +206,10 @@ export function Withdraw({
205206
try {
206207
await step3(amount.value * multiplyConsensusToSapphire)
207208
} catch (err) {
208-
setDestinationForm(prevState => ({ ...prevState, error: (err as Error).message }))
209+
setDestinationForm(prevState => ({
210+
...prevState,
211+
error: (err as BaseError).shortMessage || (err as Error).message,
212+
}))
209213
return
210214
}
211215
}

ui/src/stake/utils/errors.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
export const toErrorString = (error: Error = new Error('Unknown error')) => {
2-
let errorString = ''
1+
import { BaseError } from 'viem'
32

4-
if (Object.prototype.hasOwnProperty.call(error, 'message')) {
5-
errorString = (error as Error).message
6-
} else if (typeof error === 'object') {
7-
errorString = JSON.stringify(errorString)
8-
} else {
9-
errorString = error
10-
}
11-
12-
return errorString
3+
export const toErrorString = (error: Error | BaseError = new Error('Unknown error')) => {
4+
if ('shortMessage' in error) return error?.shortMessage
5+
if ('message' in error) return error?.message
6+
if (typeof error === 'object') return JSON.stringify(error)
7+
return error
138
}

wrap/src/components/WrapForm/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { WrapFormType } from '../../utils/types'
77
import { useInterval } from '../../hooks/useInterval'
88
import { NumberUtils } from '../../utils/number.utils'
99
import { WrapFeeWarningModal } from '../WrapFeeWarningModal'
10-
import { formatEther, parseEther } from 'viem'
10+
import { BaseError, formatEther, parseEther } from 'viem'
1111
import BigNumber from 'bignumber.js'
1212

1313
const AMOUNT_PATTERN = '^[0-9]*[.,]?[0-9]*$'
@@ -79,7 +79,10 @@ export const WrapForm: FC = () => {
7979

8080
navigate(`/wrap/tx/${txHash}?amount=${value}&action=${formType}`)
8181
} catch (ex) {
82-
setError((ex as Error)?.message || JSON.stringify(ex))
82+
// E.g. ex.shortMessage: 'User rejected the request.'
83+
// ex.message: 'User rejected the request.\n\nRequest Arguments:\n from: ...'
84+
// ex.cause.cause.code: 4001
85+
setError((ex as BaseError)?.shortMessage || (ex as Error)?.message || JSON.stringify(ex))
8386
}
8487
}
8588

0 commit comments

Comments
 (0)