Skip to content

Commit

Permalink
Merge pull request #901 from liquality/translate-liquality-errors
Browse files Browse the repository at this point in the history
fix: support liquality errors
  • Loading branch information
beemi authored Nov 3, 2022
2 parents 0a58484 + 0c70ec4 commit 1686c3a
Show file tree
Hide file tree
Showing 33 changed files with 404 additions and 216 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ VUE_APP_FIREBASE_MESSAGING_SENDER_ID=
VUE_APP_FIREBASE_APP_ID=
VUE_APP_FIREBASE_MEASUREMENT_ID=
VUE_APP_FIREBASE_DATABASE_URL=

VUE_APP_REPORT_TARGETS=Discord
VUE_APP_DISCORD_WEBHOOK=https://discord.com/api/webhooks/1034115035603161188/sO2KOyAJakks1e9_Ae-OQFSMRb8rsgl-sZ93HRDGWRWFqNJwhDhGEDJl_SfxwgkxcQHU
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
"test-full-regression": "node_modules/mocha/bin/mocha"
},
"dependencies": {
"@liquality/cryptoassets": "1.16.2",
"@liquality/cryptoassets": "1.16.3-next.3",
"@liquality/error-parser": "1.1.4-next.3",
"@liquality/ethereum-utils": "^1.13.12",
"@liquality/terra-networks": "^1.13.12",
"@liquality/types": "^1.13.12",
"@liquality/wallet-core": "1.31.3",
"@liquality/wallet-core": "1.31.4-next.3",
"@terra-money/terra.js": "^3.1.6",
"@testing-library/vue": "^5.8.2",
"amplitude-js": "8.18.2",
Expand Down
26 changes: 19 additions & 7 deletions src/broker/Background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { buildConfig } from '@liquality/wallet-core'
import { BG_PREFIX, handleConnection, removeConnectId, getRootURL } from './utils'
import { getChain } from '@liquality/cryptoassets'
import {
CUSTOM_ERRORS,
createInternalError,
reportLiqualityError,
DappNotConnectedError
} from '@liquality/error-parser'
import { connectRemote } from './terra-injection'
import { errorToLiqualityErrorString } from '@liquality/error-parser/dist/src/utils'

function attemptOrWarn(func, message) {
try {
Expand Down Expand Up @@ -154,8 +161,11 @@ class Background {
.dispatch(data.type, data.payload)
.then((result) => ({ result }))
.catch((error) => {
console.error(error)
return { error: error.message }
const liqualityErrorString = errorToLiqualityErrorString(error)
reportLiqualityError(error)
return {
error: liqualityErrorString
}
})
.then((response) =>
attemptOrWarn(
Expand All @@ -175,7 +185,7 @@ class Background {
break

default:
throw new Error(`Received an invalid message type: ${type}`)
throw createInternalError(CUSTOM_ERRORS.Invalid.MessageType(type))
}
}

Expand Down Expand Up @@ -247,7 +257,7 @@ class Background {
connection.postMessage({
id,
data: {
error: 'Use enable() method first'
error: new DappNotConnectedError({ dapp: origin, chain: chain }).toString()
}
})
}
Expand All @@ -260,7 +270,7 @@ class Background {
connection.postMessage({
id,
data: {
error: 'Use enable() method first'
error: new DappNotConnectedError({ dapp: origin, chain: chain }).toString()
}
})
}
Expand All @@ -278,8 +288,10 @@ class Background {
.dispatch(action, data)
.then((result) => ({ result }))
.catch((error) => {
console.error(error) /* eslint-disable-line */
return { error: error.toString() }
const liqualityErrorString = errorToLiqualityErrorString(error)
return {
error: liqualityErrorString
}
})
.then((response) => {
connection.postMessage({
Expand Down
5 changes: 3 additions & 2 deletions src/broker/Foreground.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CUSTOM_ERRORS, createInternalError } from '@liquality/error-parser'
import EventEmitter from 'events'
import { BG_PREFIX, connectToBackground, newConnectId, Deferred } from './utils'

Expand Down Expand Up @@ -60,7 +61,7 @@ class Foreground {
break

case 'REHYDRATE_STATE':
if (this.initialized) throw new Error('State has already been synchronised with Background')
if (this.initialized) throw createInternalError(CUSTOM_ERRORS.FailedAssert.RehydrateState)

this.store.replaceState(data)

Expand All @@ -78,7 +79,7 @@ class Foreground {
break

default:
throw new Error(`Received an invalid message type: ${type}`)
throw createInternalError(CUSTOM_ERRORS.Invalid.MessageType(type))
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/components/CustomFeesEIP1559.vue
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ import { prettyFiatBalance } from '@liquality/wallet-core/dist/src/utils/coinFor
import ChevronUpIcon from '@/assets/icons/chevron_up.svg'
import ChevronDownIcon from '@/assets/icons/chevron_down.svg'
import { mapState } from 'vuex'
import {
NoTipError,
VeryLowTipError,
VeryHighTipWarning,
NoMaxFeeError,
VeryHighMaxFeeWarning
} from '@liquality/error-parser/dist/src/LiqualityErrors'
export default {
components: {
Expand Down Expand Up @@ -266,20 +273,20 @@ export default {
...mapState(['activeNetwork']),
// TODO: move erro handling to wallet-core
noTipError() {
return !this.tipFee ? 'Miner tip must be greater than 0 GWEI' : null
return !this.tipFee ? this.$tle(new NoTipError()) : null
},
veryLowTipError() {
return !this.noTipError && this.tipFee < this.fees.slow.fee.maxPriorityFeePerGas
? 'Miner tip is extremely low and the transaction could fail. Use ‘Low’.'
? this.$tle(new VeryLowTipError())
: null
},
veryHighTipWarning() {
return this.tipFee > this.fees.fast.fee.maxPriorityFeePerGas
? 'Miner tip is higher than necessary. You may pay more than needed. Use ‘High’.'
? this.$tle(new VeryHighTipWarning())
: null
},
noMaxFeeError() {
return !this.maxFee ? 'Max fee must be greater than 0 GWEI' : null
return !this.maxFee ? this.$tle(new NoMaxFeeError()) : null
},
veryLowMaxFeeError() {
return this.maxFee < this.fees.slow.fee.maxFeePerGas
Expand All @@ -288,7 +295,7 @@ export default {
},
veryHighFeeWarning() {
return this.maxFee > this.fees.fast.fee.maxFeePerGas
? `Max fee is higher than necessary ${this.fees.fast.fee.maxFeePerGas} GWEI (Base Fee plus Miner Tip). Review your maximum ‘New Fee Total’.`
? this.$tle(new VeryHighMaxFeeWarning({ maxFeePerGas: this.fees.fast.fee.maxFeePerGas }))
: null
},
slowPreset() {
Expand Down
4 changes: 3 additions & 1 deletion src/pageProvider/bitcoinProvider.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UserDeclinedError } from '@liquality/error-parser'
import { PageProvider } from './pageProvider'

const BITCOIN_REQUEST_MAP = {
Expand All @@ -23,7 +24,8 @@ class BitcoinPageProvider extends PageProvider {
this.window.bitcoin = {
enable: async () => {
const { accepted } = await this.window.providerManager.enable('bitcoin')
if (!accepted) throw new Error('User rejected')
if (!accepted) throw new UserDeclinedError()

const btc = this.window.providerManager.getProviderFor('BTC')
return btc.getMethod('wallet.getAddresses')()
},
Expand Down
3 changes: 2 additions & 1 deletion src/pageProvider/ethereumProvider.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UserDeclinedError } from '@liquality/error-parser'
import { PageProvider } from './pageProvider'

class EthereumPageProvider extends PageProvider {
Expand Down Expand Up @@ -72,7 +73,7 @@ class EthereumPageProvider extends PageProvider {
chainId: this.network.chainId.toString(16),
enable: async () => {
const { accepted } = await this.window.providerManager.enable(this.chain)
if (!accepted) throw new Error('User rejected')
if (!accepted) throw new UserDeclinedError()
return this.getAddresses()
},
request: async (req) => {
Expand Down
3 changes: 2 additions & 1 deletion src/pageProvider/globalEthereumProvider.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UserDeclinedError } from '@liquality/error-parser'
import { PageProvider } from './pageProvider'

class GlobalEthereumPageProvider extends PageProvider {
Expand All @@ -12,7 +13,7 @@ class GlobalEthereumPageProvider extends PageProvider {
async enable() {
// TODO: explicityl pass provider maanger?
const { accepted, chain } = await this.window.providerManager.enable()
if (!accepted) throw new Error('User rejected')
if (!accepted) throw new UserDeclinedError()
const chainChanged = this.window.ethereumProxyChain !== chain
this.window.ethereumProxyChain = chain
const injectionName = this.window.providerManager.getInjectionName(chain)
Expand Down
3 changes: 2 additions & 1 deletion src/pageProvider/nearProvider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { COMMON_REQUEST_MAP } from './utils'
import { PageProvider } from './pageProvider'
import { UserDeclinedError } from '@liquality/error-parser'

class NearPageProvider extends PageProvider {
async handleRequest(req) {
Expand All @@ -12,7 +13,7 @@ class NearPageProvider extends PageProvider {
this.window.near = {
enable: async () => {
const { accepted } = await this.window.providerManager.enable('near')
if (!accepted) throw new Error('User rejected')
if (!accepted) throw new UserDeclinedError()
const near = this.window.providerManager.getProviderFor('NEAR')
return near.getMethod('wallet.getAddresses')()
},
Expand Down
4 changes: 3 additions & 1 deletion src/pageProvider/pageProvider.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { CUSTOM_ERRORS, createInternalError } from '@liquality/error-parser'

class PageProvider {
window
constructor(window) {
this.window = window
}
setup() {
throw new Error('not implemented')
throw createInternalError(CUSTOM_ERRORS.Unimplemented.Method)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/pageProvider/solanaProvider.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UserDeclinedError } from '@liquality/error-parser'
import { PageProvider } from './pageProvider'
import { COMMON_REQUEST_MAP } from './utils'

Expand All @@ -11,7 +12,7 @@ class SolanaPageProvider extends PageProvider {
this.window.sollet = {
enable: async () => {
const { accepted } = await this.window.providerManager.enable('solana')
if (!accepted) throw new Error('User rejected')
if (!accepted) throw new UserDeclinedError()
const solana = this.window.providerManager.getProviderFor('SOL')
return solana.getMethod('wallet.getAddresses')()
},
Expand Down
3 changes: 2 additions & 1 deletion src/pageProvider/terraProvider.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UserDeclinedError } from '@liquality/error-parser'
import { PageProvider } from './pageProvider'
import { COMMON_REQUEST_MAP } from './utils'

Expand All @@ -13,7 +14,7 @@ class TerraPageProvider extends PageProvider {
this.window.terra = {
enable: async () => {
const accepted = await this.window.providerManager.enable('terra')
if (!accepted) throw new Error('User rejected')
if (!accepted) throw new UserDeclinedError()
const terra = this.window.providerManager.getProviderFor('LUNA')
return terra.getMethod('wallet.getAddresses')()
},
Expand Down
3 changes: 2 additions & 1 deletion src/store/modules/app/handlePaymentUri.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import qs from 'qs'
import { currencyToUnit } from '@liquality/cryptoassets'
import { CUSTOM_ERRORS, createInternalError } from '@liquality/error-parser'

export const handlePaymentUri = async ({ dispatch, rootGetters }, { data }) => {
const { uri } = data
Expand All @@ -11,7 +12,7 @@ export const handlePaymentUri = async ({ dispatch, rootGetters }, { data }) => {
bitcoin: 'BTC'
}[parsed.protocol.replace(':', '')]

if (!asset) throw new Error('Unsupported payment URI')
if (!asset) throw createInternalError(CUSTOM_ERRORS.Unsupported.PaymentURI)

const address = parsed.pathname
const params = qs.parse(parsed.search.replace('?', ''))
Expand Down
3 changes: 2 additions & 1 deletion src/store/modules/app/replyPermission.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { errorToLiqualityErrorString } from '@liquality/error-parser/dist/src/utils'
import { emitter } from '../../utils'

export const replyPermission = async ({ dispatch }, { request, allowed }) => {
Expand All @@ -6,7 +7,7 @@ export const replyPermission = async ({ dispatch }, { request, allowed }) => {
try {
response.result = await dispatch('executeRequest', { request })
} catch (error) {
response.error = error.message
response.error = errorToLiqualityErrorString(error)
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/store/modules/app/requestOriginAccess.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { stringify } from 'qs'
import { emitter } from '../../utils'
import { createPopup } from '../../../broker/utils'
import { UserDeclinedError } from '@liquality/error-parser'

export const requestOriginAccess = async (
{ state, dispatch, commit },
Expand Down Expand Up @@ -31,14 +32,14 @@ export const requestOriginAccess = async (
chain
})
} else {
reject(new Error('User denied'))
reject(new UserDeclinedError())
}
})

const query = stringify({ origin, chain })
createPopup(`/enable?${query}`, () => {
commit('SET_ORIGIN_ACCESS_ACTIVE', { active: false })
reject(new Error('User denied'))
reject(new UserDeclinedError())
})
})
}
Expand Down
18 changes: 13 additions & 5 deletions src/store/modules/app/requestPermission.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { stringify } from 'qs'
import { emitter } from '../../utils'
import { createPopup } from '../../../broker/utils'
import { ChainId } from '@liquality/cryptoassets'
import {
CUSTOM_ERRORS,
createInternalError,
NoActiveWalletError,
WalletLockedError,
UserDeclinedError
} from '@liquality/error-parser'

const CONFIRM_REQUIRED = [
/^wallet.buildTransaction$/,
Expand Down Expand Up @@ -36,12 +43,13 @@ export const requestPermission = async (
if (!requestPermissionActive) {
commit('SET_REQUEST_PERMISSION_ACTIVE', { active: true })
await dispatch('requestUnlockWallet')
if (!rootState.unlockedAt) throw new Error('Wallet is locked. Unlock the wallet first.')
if (!rootState.activeWalletId) throw new Error('No active wallet found. Create a wallet first.')
if (!rootState.unlockedAt) throw new WalletLockedError()
if (!rootState.activeWalletId) throw new NoActiveWalletError()

let { asset, accountId, method, args, chain } = data

if (!ALLOWED.some((re) => re.test(method))) throw new Error('Method not allowed')
if (!ALLOWED.some((re) => re.test(method)))
throw createInternalError(CUSTOM_ERRORS.Unsupported.Method)

const { activeNetwork: network, activeWalletId: walletId } = rootState

Expand Down Expand Up @@ -72,7 +80,7 @@ export const requestPermission = async (
return new Promise((resolve, reject) => {
commit('SET_REQUEST_PERMISSION_ACTIVE', { active: false })
emitter.$once(`permission:${id}`, (response) => {
if (!response.allowed) reject(new Error('User denied'))
if (!response.allowed) reject(new UserDeclinedError())
if (response.error) reject(new Error(response.error))
resolve(response.result)
})
Expand All @@ -95,7 +103,7 @@ export const requestPermission = async (
permissionRoute = '/permission/signPsbt'
}

createPopup(`${permissionRoute}?${query}`, () => reject(new Error('User denied')))
createPopup(`${permissionRoute}?${query}`, () => reject(new UserDeclinedError()))
})
} else {
commit('SET_REQUEST_PERMISSION_ACTIVE', { active: false })
Expand Down
9 changes: 4 additions & 5 deletions src/store/modules/app/requestUnlockWallet.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { stringify } from 'qs'
import { emitter } from '../../utils'
import { createPopup } from '../../../broker/utils'
import { NoActiveWalletError, WalletLockedError } from '@liquality/error-parser'

export const requestUnlockWallet = async ({ rootState }) => {
if (!rootState.activeWalletId) throw new Error('No active wallet found. Create a wallet first.')
if (!rootState.activeWalletId) throw new NoActiveWalletError()

if (!rootState.unlockedAt) {
const id = Date.now() + '.' + Math.random()
return new Promise((resolve, reject) => {
emitter.$once(`unlocked:${id}`, (unlocked) => {
if (unlocked) resolve()
else reject(new Error('Wallet is locked. Unlock the wallet first.'))
else reject(new WalletLockedError())
})
const query = stringify({ id })

createPopup(`/request-unlock?${query}`, () =>
reject(new Error('Wallet is locked. Unlock the wallet first.'))
)
createPopup(`/request-unlock?${query}`, () => reject(new WalletLockedError()))
})
}
}
Loading

0 comments on commit 1686c3a

Please sign in to comment.