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

fix(wallet-mobile): NetworkError instance #3213

Merged
merged 5 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {NavigationRouteContext} from '@react-navigation/native'
import {action} from '@storybook/addon-actions'
import {storiesOf} from '@storybook/react-native'
import {Api} from '@yoroi/types'
import React from 'react'

import {WalletManager, walletManager} from '../../../../wallet-manager/walletManager'
import {WalletManagerProvider} from '../../../../wallet-manager/WalletManagerContext'
import {NetworkError} from '../../../../yoroi-wallets/cardano/errors'
import {SaveNanoXScreen} from './SaveNanoXScreen'

storiesOf('SaveNanoXScreen', module)
Expand Down Expand Up @@ -33,7 +33,7 @@ storiesOf('SaveNanoXScreen', module)
createWalletWithBip44Account: async (...args: unknown[]) => {
action('createWalletWithBip44Account')(...args)
await new Promise((resolve) => setTimeout(resolve, 1000))
throw new NetworkError()
throw new Api.Errors.Network()
},
} as unknown as WalletManager
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {useSetupWallet} from '@yoroi/setup-wallet'
import {Api} from '@yoroi/types'
import React from 'react'
import {defineMessages, useIntl} from 'react-intl'
import {InteractionManager} from 'react-native'
Expand All @@ -9,7 +10,6 @@ import {errorMessages} from '../../../../i18n/global-messages'
import {useMetrics} from '../../../../metrics/metricsManager'
import {useWalletNavigation} from '../../../../navigation'
import {AddressMode} from '../../../../wallet-manager/types'
import {NetworkError} from '../../../../yoroi-wallets/cardano/errors'
import {useCreateBip44Wallet} from '../../../../yoroi-wallets/hooks'
import {WalletImplementationId} from '../../../../yoroi-wallets/types'
import {WalletNameForm} from '../WalletNameForm/WalletNameForm'
Expand All @@ -30,7 +30,7 @@ export const SaveNanoXScreen = () => {
},
onError: (error) => {
InteractionManager.runAfterInteractions(() => {
return error instanceof NetworkError
return error instanceof Api.Errors.Network
? showErrorDialog(errorMessages.networkError, intl)
: showErrorDialog(errorMessages.generalError, intl, {message: error.message})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {NavigationRouteContext} from '@react-navigation/native'
import {action} from '@storybook/addon-actions'
import {storiesOf} from '@storybook/react-native'
import {Api} from '@yoroi/types'
import React from 'react'

import {WalletManager, walletManager} from '../../../../wallet-manager/walletManager'
import {WalletManagerProvider} from '../../../../wallet-manager/WalletManagerContext'
import {NetworkError} from '../../../../yoroi-wallets/cardano/errors'
import {SaveReadOnlyWalletScreen} from './SaveReadOnlyWalletScreen'

storiesOf('SaveReadOnlyWalletScreen', module)
Expand Down Expand Up @@ -50,7 +50,7 @@ storiesOf('SaveReadOnlyWalletScreen', module)
createWalletWithBip44Account: async (...args: unknown[]) => {
action('createWalletWithBip44Account')(...args)
await new Promise((resolve) => setTimeout(resolve, 1000))
throw new NetworkError()
throw new Api.Errors.Network()
},
} as unknown as WalletManager
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {useSetupWallet} from '@yoroi/setup-wallet'
import {Api} from '@yoroi/types'
import React from 'react'
import {defineMessages, useIntl} from 'react-intl'
import {FlatList, InteractionManager, ScrollView, StyleSheet, View} from 'react-native'
Expand All @@ -12,7 +13,6 @@ import {useWalletNavigation} from '../../../../navigation'
import {theme} from '../../../../theme'
import {isEmptyString} from '../../../../utils/utils'
import {AddressMode} from '../../../../wallet-manager/types'
import {NetworkError} from '../../../../yoroi-wallets/cardano/errors'
import {NUMBERS} from '../../../../yoroi-wallets/cardano/numbers'
import {useCreateBip44Wallet, usePlate} from '../../../../yoroi-wallets/hooks'
import {NetworkId, WalletImplementationId} from '../../../../yoroi-wallets/types'
Expand Down Expand Up @@ -43,7 +43,7 @@ export const SaveReadOnlyWalletScreen = () => {
},
onError: (error) => {
InteractionManager.runAfterInteractions(() => {
return error instanceof NetworkError
return error instanceof Api.Errors.Network
? showErrorDialog(errorMessages.networkError, intl)
: showErrorDialog(errorMessages.generalError, intl, {message: error.message})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {useFocusEffect} from '@react-navigation/native'
import {NetworkError, useAsyncStorage} from '@yoroi/common'
import {useAsyncStorage} from '@yoroi/common'
import {useSetupWallet} from '@yoroi/setup-wallet'
import {useTheme} from '@yoroi/theme'
import {Api} from '@yoroi/types'
import * as React from 'react'
import {useIntl} from 'react-intl'
import {
Expand Down Expand Up @@ -121,7 +122,7 @@ export const WalletDetailsScreen = () => {
InteractionManager.runAfterInteractions(() => {
return error instanceof InvalidState
? showErrorDialog(errorMessages.walletStateInvalid, intl)
: error instanceof NetworkError
: error instanceof Api.Errors.Network
? showErrorDialog(errorMessages.networkError, intl)
: showErrorDialog(errorMessages.generalError, intl, {message: error.message})
})
Expand All @@ -144,7 +145,7 @@ export const WalletDetailsScreen = () => {
},
onError: (error) => {
InteractionManager.runAfterInteractions(() => {
return error instanceof NetworkError
return error instanceof Api.Errors.Network
? showErrorDialog(errorMessages.networkError, intl)
: showErrorDialog(errorMessages.generalError, intl, {message: error.message})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {useFocusEffect} from '@react-navigation/native'
import {NetworkError, useAsyncStorage} from '@yoroi/common'
import {useAsyncStorage} from '@yoroi/common'
import {useSetupWallet} from '@yoroi/setup-wallet'
import {useTheme} from '@yoroi/theme'
import {Api} from '@yoroi/types'
import * as React from 'react'
import {useIntl} from 'react-intl'
import {
Expand Down Expand Up @@ -110,7 +111,7 @@ export const RestoreWalletDetailsScreen = () => {
InteractionManager.runAfterInteractions(() => {
return error instanceof InvalidState
? showErrorDialog(errorMessages.walletStateInvalid, intl)
: error instanceof NetworkError
: error instanceof Api.Errors.Network
? showErrorDialog(errorMessages.networkError, intl)
: showErrorDialog(errorMessages.generalError, intl, {message: error.message})
})
Expand All @@ -134,7 +135,7 @@ export const RestoreWalletDetailsScreen = () => {
},
onError: (error) => {
InteractionManager.runAfterInteractions(() => {
return error instanceof NetworkError
return error instanceof Api.Errors.Network
? showErrorDialog(errorMessages.networkError, intl)
: showErrorDialog(errorMessages.generalError, intl, {message: error.message})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {walletChecksum} from '@emurgo/cip4-js'
import {useFocusEffect, useNavigation} from '@react-navigation/native'
import {NetworkError} from '@yoroi/common'
import {useSetupWallet} from '@yoroi/setup-wallet'
import {useTheme} from '@yoroi/theme'
import {Api} from '@yoroi/types'
import * as React from 'react'
import {useIntl} from 'react-intl'
import {InteractionManager, StyleSheet, Text, View} from 'react-native'
Expand Down Expand Up @@ -62,7 +62,7 @@ export const RestoreWalletScreen = () => {
InteractionManager.runAfterInteractions(() => {
return error instanceof InvalidState
? showErrorDialog(errorMessages.walletStateInvalid, intl)
: error instanceof NetworkError
: error instanceof Api.Errors.Network
? showErrorDialog(errorMessages.networkError, intl)
: showErrorDialog(errorMessages.generalError, intl, {message: error.message})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {action} from '@storybook/addon-actions'
import {storiesOf} from '@storybook/react-native'
import {Api} from '@yoroi/types'
import React from 'react'

import {WalletMeta} from '../../../wallet-manager/types'
import {mockWalletManager, WalletManager} from '../../../wallet-manager/walletManager'
import {WalletManagerProvider} from '../../../wallet-manager/WalletManagerContext'
import {InvalidState, NetworkError} from '../../../yoroi-wallets/cardano/errors'
import {InvalidState} from '../../../yoroi-wallets/cardano/errors'
import {mocks} from '../../../yoroi-wallets/mocks'
import {SelectWalletFromList} from './SelectWalletFromListScreen'

Expand Down Expand Up @@ -59,7 +60,7 @@ storiesOf('SelectWalletFromList', module)
openWallet: async (walletMeta: WalletMeta) => {
action('openWallet')(walletMeta)
await delay(1000)
throw new NetworkError()
throw new Api.Errors.Network()
},
} as unknown as WalletManager
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {useFocusEffect, useNavigation} from '@react-navigation/native'
import {NetworkError} from '@yoroi/common'
import {useSetupWallet} from '@yoroi/setup-wallet'
import {useTheme} from '@yoroi/theme'
import {Api} from '@yoroi/types'
import * as React from 'react'
import {useIntl} from 'react-intl'
import {InteractionManager, Linking, RefreshControl, StyleSheet, Text, TouchableOpacity, View} from 'react-native'
Expand Down Expand Up @@ -59,7 +59,7 @@ export const SelectWalletFromList = () => {
InteractionManager.runAfterInteractions(() => {
return error instanceof InvalidState
? showErrorDialog(errorMessages.walletStateInvalid, intl)
: error instanceof NetworkError
: error instanceof Api.Errors.Network
? showErrorDialog(errorMessages.networkError, intl)
: showErrorDialog(errorMessages.generalError, intl, {message: error.message})
})
Expand Down
4 changes: 2 additions & 2 deletions apps/wallet-mobile/src/legacy/DeveloperScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
import {useNavigation} from '@react-navigation/native'
import {useTheme} from '@yoroi/theme'
import {Api} from '@yoroi/types'
import assert from 'assert'
import ExtendableError from 'es6-error'
import _ from 'lodash'
Expand All @@ -19,7 +20,6 @@ import {errorMessages} from '../i18n/global-messages'
import {storageVersionMaker} from '../migrations/storageVersion'
import {AppRoutes, useWalletNavigation} from '../navigation'
import {isEmptyString} from '../utils/utils'
import {NetworkError} from '../yoroi-wallets/cardano/errors'
import {generateAdaMnemonic} from '../yoroi-wallets/cardano/mnemonic'
import {useCreateWallet} from '../yoroi-wallets/hooks'
import {rootStorage} from '../yoroi-wallets/storage/rootStorage'
Expand All @@ -45,7 +45,7 @@ export const DeveloperScreen = () => {
onSuccess: () => resetToWalletSelection(),
onError: (error) => {
InteractionManager.runAfterInteractions(() => {
return error instanceof NetworkError
return error instanceof Api.Errors.Network
? showErrorDialog(errorMessages.networkError, intl)
: showErrorDialog(errorMessages.generalError, intl, {message: error.message})
})
Expand Down
5 changes: 3 additions & 2 deletions apps/wallet-mobile/src/yoroi-wallets/cardano/api/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {Api} from '@yoroi/types'
import {Platform} from 'react-native'
import DeviceInfo from 'react-native-device-info'

import {Logger} from '../../logging'
import type {BackendConfig} from '../../types/other'
import {ApiError, ApiHistoryError, NetworkError} from '../errors'
import {ApiError, ApiHistoryError} from '../errors'

type RequestMethod = 'POST' | 'GET'

Expand Down Expand Up @@ -61,7 +62,7 @@ export const checkedFetch = (request: FetchRequest<any>) => {
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
*/
if (e instanceof TypeError) {
throw new NetworkError()
throw new Api.Errors.Network()
}

throw e
Expand Down
10 changes: 0 additions & 10 deletions apps/wallet-mobile/src/yoroi-wallets/cardano/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ export class ApiError extends LocalizableError {
}
}

// thrown when api failed to connect to the server
export class NetworkError extends LocalizableError {
constructor() {
super({
id: errorMessages.networkError.message.id,
defaultMessage: errorMessages.networkError.message.defaultMessage || '',
})
}
}

// thrown by the backend after a rollback
export class ApiHistoryError extends ApiError {
public static readonly errors = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"start": {
"line": 80,
"column": 30,
"index": 2604
"index": 2577
},
"end": {
"line": 83,
"column": 3,
"index": 2741
"index": 2714
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"start": {
"line": 143,
"column": 21,
"index": 4260
"index": 4233
},
"end": {
"line": 146,
"column": 3,
"index": 4387
"index": 4360
}
},
{
Expand All @@ -21,12 +21,12 @@
"start": {
"line": 147,
"column": 17,
"index": 4406
"index": 4379
},
"end": {
"line": 150,
"column": 3,
"index": 4520
"index": 4493
}
},
{
Expand All @@ -36,12 +36,12 @@
"start": {
"line": 151,
"column": 22,
"index": 4544
"index": 4517
},
"end": {
"line": 154,
"column": 3,
"index": 4668
"index": 4641
}
},
{
Expand All @@ -51,12 +51,12 @@
"start": {
"line": 155,
"column": 7,
"index": 4677
"index": 4650
},
"end": {
"line": 158,
"column": 3,
"index": 4775
"index": 4748
}
},
{
Expand All @@ -66,12 +66,12 @@
"start": {
"line": 159,
"column": 18,
"index": 4795
"index": 4768
},
"end": {
"line": 162,
"column": 3,
"index": 4916
"index": 4889
}
}
]
8 changes: 3 additions & 5 deletions packages/common/src/api/fetcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// fetcher.test.ts

import {fetcher} from './fetcher' // adjust the import to your file structure
import {ApiError, NetworkError} from '../errors/errors' // adjust the import to your file structure
import {ApiError} from '../errors/errors' // adjust the import to your file structure
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import {Api} from '@yoroi/types'

const mock = new MockAdapter(axios)

Expand Down Expand Up @@ -38,10 +39,7 @@ describe('fetcher', () => {

await expect(
fetcher({url: '/network-error', method: 'GET'}),
).rejects.toThrow(NetworkError)
await expect(
fetcher({url: '/network-error', method: 'GET'}),
).rejects.toHaveProperty('message', 'No response received')
).rejects.toThrow(Api.Errors.Network)
})

it('throws generic Error for other cases', async () => {
Expand Down
Loading
Loading