Skip to content

Commit a990059

Browse files
committed
Using more general errors
1 parent 6c33c60 commit a990059

File tree

10 files changed

+23
-99
lines changed

10 files changed

+23
-99
lines changed

src/components/Swap/Swap.svelte

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import approveAllowance from '../../libs/token/approveAllowance'
1111
import { account } from '../../stores/account'
1212
import Loading from '../Loading/Loading.svelte'
13+
import notifyError from '../utils/notifyError'
1314
1415
let tokenFrom: Token
1516
let amountFrom: bigint
@@ -57,16 +58,7 @@
5758
canTrade = true
5859
} catch (err) {
5960
console.error(err)
60-
61-
let reason = 'Unknown'
62-
63-
if (err instanceof Error) {
64-
reason = err.message
65-
} else if (typeof err === 'string') {
66-
reason = err
67-
}
68-
69-
errorToast(`There was an error fetching the price. ${reason}`)
61+
notifyError(err, 'There was an error fetching the price')
7062
} finally {
7163
gettingPrice = false
7264
}
@@ -103,17 +95,7 @@
10395
}
10496
} catch (err) {
10597
console.error(err)
106-
107-
if (err instanceof Error) {
108-
errorToast(err.message)
109-
} else if (err && typeof err === 'object' && 'validationErrors' in err && Array.isArray(err.validationErrors)) {
110-
const validationError = err.validationErrors[0]
111-
errorToast(validationError.description)
112-
} else if (typeof err === 'string') {
113-
errorToast(err)
114-
} else {
115-
errorToast('There was an error trading the tokens')
116-
}
98+
notifyError(err, 'There was an error trading the tokens')
11799
} finally {
118100
trading = false
119101
}

src/components/Swap/TokenAmount.svelte

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
import getBalance from '../../libs/token/getBalance'
1111
import { account } from '../../stores/account'
1212
import { network } from '../../stores/network'
13-
import { NoAccountAddressError, NotConnectedError } from '../../libs/error'
1413
import type { FetchBalanceResult } from '@wagmi/core'
1514
import { truncateString } from '../../libs/utils/truncateString'
15+
import notifyError from '../utils/notifyError'
1616
1717
export let token: Token
1818
export let amount: bigint
@@ -61,17 +61,7 @@
6161
})
6262
} catch (err) {
6363
console.error(err)
64-
65-
switch (true) {
66-
case err instanceof NotConnectedError:
67-
// TODO
68-
break
69-
case err instanceof NoAccountAddressError:
70-
// TODO
71-
break
72-
default:
73-
// TODO
74-
}
64+
notifyError(err, 'There was an error fetching the balance')
7565
}
7666
}
7767

src/components/utils/notifyError.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { errorToast } from "../NotificationToast"
2+
3+
export default function notifyError(err: unknown, defaultMsg: string) {
4+
if (err instanceof Error) {
5+
errorToast(err.message)
6+
} else if (err && typeof err === 'object' && 'validationErrors' in err && Array.isArray(err.validationErrors)) {
7+
const validationError = err.validationErrors[0]
8+
errorToast(validationError.description)
9+
} else if (typeof err === 'string') {
10+
errorToast(err)
11+
} else {
12+
errorToast(defaultMsg)
13+
}
14+
}

src/libs/api/0x.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ async function OxApi({ apiCall, sellToken, buyToken, sellAmount, chain }: OxApiA
1717

1818
const response = await fetch(`api/0x?${queryParams}`)
1919

20-
// TODO: handle errors (e.g. Insifficient asset liquidity, etc...)
21-
// if (!response.ok) throw new Error(`Failed to request: ${response.statusText}`)
2220
if (!response.ok) {
2321
const error = await getError(response)
2422
throw error

src/libs/error/errors.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/libs/error/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/libs/token/getBalance.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { fetchBalance, type Address, type Unit } from '@wagmi/core'
22
import checkConnected from '../utils/checkConnected'
33
import { get } from 'svelte/store'
44
import { account } from '../../stores/account'
5-
import { NoAccountAddressError } from '../error'
65

76
type GetBalanceArgs = {
87
// Address of balance to check
@@ -27,7 +26,7 @@ export default async function getBalance({ address, chainId, formatUnits, token
2726
if ($account.address) {
2827
userAddress = $account.address
2928
} else {
30-
throw new NoAccountAddressError()
29+
throw new Error('No user address has been provided')
3130
}
3231
}
3332

src/libs/utils/checkConnected.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { get } from 'svelte/store'
22
import { account } from '../../stores/account'
3-
import { NotConnectedError } from '../error'
43

54
export default function checkConnected() {
65
const $account = get(account)
76

87
if (!$account?.isConnected) {
9-
throw new NotConnectedError()
8+
throw new Error('No wallet connected')
109
}
1110

1211
return $account

src/libs/utils/getConnectedWallet.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { getWalletClient } from '@wagmi/core'
2-
import { NotConnectedError } from '../error'
32

43
export default async function getConnectedWallet(chainId?: number) {
54
const walletClient = await getWalletClient({ chainId })
65

76
if (!walletClient) {
8-
throw new NotConnectedError()
7+
throw new Error('No wallet connected')
98
}
109

1110
return walletClient

src/libs/web3/wagmi.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { w3mConnectors, w3mProvider } from '@web3modal/ethereum'
22
import { configureChains, createConfig } from '@wagmi/core'
33
import { PUBLIC_WALLET_CONNECT_PROJECT_ID } from '$env/static/public'
44
import { chains } from './chains'
5-
import { NotSupportedChainError } from '../error'
65

76
const projectId = PUBLIC_WALLET_CONNECT_PROJECT_ID
87

@@ -20,6 +19,6 @@ export function isChainSupported(chainId: number | string) {
2019

2120
export function checkSupportedChain(chainId: number) {
2221
if (!isChainSupported(chainId)) {
23-
throw new NotSupportedChainError()
22+
throw new Error(`Chain ${chainId} is not supported`)
2423
}
2524
}

0 commit comments

Comments
 (0)