Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
v-almonacid committed Sep 16, 2020
1 parent 3c9ff9d commit 0ed84f8
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 139 deletions.
1 change: 0 additions & 1 deletion android/app/src/dev/res/xml/network_security_config.xml
Expand Up @@ -4,6 +4,5 @@
<domain includeSubdomains="false">localhost</domain>
<domain includeSubdomains="false">10.0.2.2</domain>
<domain includeSubdomains="false">10.0.3.2</domain>
<domain includeSubdomains="true">192.168.1.74</domain>
</domain-config>
</network-security-config>
4 changes: 2 additions & 2 deletions src/actions.js
Expand Up @@ -449,9 +449,9 @@ const showDialog = (translations: DialogOptions): Promise<DialogButton> =>
})

export const showErrorDialog = (
dialog: Object,
dialog: {title: Object, message: Object},
intl: ?intlShape,
msgOptions?: Object,
msgOptions?: {message: string},
): Promise<DialogButton> => {
let title, message, yesButton
if (intl != null) {
Expand Down
1 change: 0 additions & 1 deletion src/api/fetch.js
Expand Up @@ -10,7 +10,6 @@ import type {NetworkConfig} from '../config/types'
type RequestMethod = 'POST' | 'GET'

const _checkResponse = (status, responseBody, requestPayload) => {
// TODO: verify that this is the correct behaviour
if (status !== 200) {
if (
responseBody.error?.response === 'REFERENCE_TX_NOT_FOUND' ||
Expand Down
53 changes: 20 additions & 33 deletions src/components/Delegation/DelegationConfirmation.js
Expand Up @@ -6,7 +6,6 @@ import {connect} from 'react-redux'
import {withStateHandlers, withHandlers} from 'recompose'
import {injectIntl, defineMessages} from 'react-intl'
import {BigNumber} from 'bignumber.js'
import {ErrorCodes} from '@cardano-foundation/ledgerjs-hw-app-cardano'

import {
easyConfirmationSelector,
Expand All @@ -15,7 +14,6 @@ import {
} from '../../selectors'
import {
showErrorDialog,
handleGeneralError,
submitDelegationTx,
submitSignedTx,
} from '../../actions'
Expand Down Expand Up @@ -44,14 +42,11 @@ import {
import {NetworkError, ApiError} from '../../api/errors'
import {WrongPassword} from '../../crypto/errors'
import walletManager, {SystemAuthDisabled} from '../../crypto/walletManager'
import {
GeneralConnectionError,
LedgerUserError,
} from '../../crypto/shelley/ledgerUtils'
import KeyStore from '../../crypto/KeyStore'
import LedgerTransportSwitchModal from '../Ledger/LedgerTransportSwitchModal'
import LedgerConnect from '../Ledger/LedgerConnect'
import HWInstructions from '../Ledger/HWInstructions'
import LocalizableError from '../../i18n/LocalizableError'

import styles from './styles/DelegationConfirmation.style'

Expand Down Expand Up @@ -80,10 +75,6 @@ const messages = defineMessages({
'!!!Current approximation of rewards that you will ' +
'receive per epoch:',
},
delegationTxSignError: {
id: 'components.stakingcenter.confirmDelegation.delegationTxSignError',
defaultMessage: '!!!Error while signing delegation transaction',
},
})

/**
Expand Down Expand Up @@ -132,7 +123,9 @@ const handleOnConfirm = async (
if (e instanceof NetworkError) {
await showErrorDialog(errorMessages.networkError, intl)
} else if (e instanceof ApiError) {
await showErrorDialog(errorMessages.apiError, intl)
await showErrorDialog(errorMessages.apiError, intl, {
message: JSON.stringify(e.request),
})
} else {
throw e
}
Expand All @@ -150,19 +143,17 @@ const handleOnConfirm = async (
)
await submitTx(Buffer.from(signedTx.encodedTx).toString('base64'))
} catch (e) {
if (e.statusCode === ErrorCodes.ERR_REJECTED_BY_USER) {
return
} else if (
e instanceof GeneralConnectionError ||
e instanceof LedgerUserError
) {
await showErrorDialog(errorMessages.hwConnectionError, intl)
if (e instanceof LocalizableError) {
await showErrorDialog(errorMessages.generalTxError, intl, {
message: intl.formatMessage({
id: e.id,
defaultMessage: e.defaultMessage,
}),
})
} else {
handleGeneralError(
intl.formatMessage(messages.delegationTxSignError),
e,
intl,
)
await showErrorDialog(errorMessages.generalTxError, intl, {
message: String(e.message),
})
}
} finally {
setProcessingTx(false)
Expand Down Expand Up @@ -190,11 +181,9 @@ const handleOnConfirm = async (

return
} else {
handleGeneralError(
intl.formatMessage(messages.delegationTxSignError),
e,
intl,
)
await showErrorDialog(errorMessages.generalTxError, intl, {
message: String(e.message),
})
}
}

Expand All @@ -215,11 +204,9 @@ const handleOnConfirm = async (
if (e instanceof WrongPassword) {
await showErrorDialog(errorMessages.incorrectPassword, intl)
} else {
handleGeneralError(
intl.formatMessage(messages.delegationTxSignError),
e,
intl,
)
await showErrorDialog(errorMessages.generalTxError, intl, {
message: String(e.message),
})
}
}
}
Expand Down
22 changes: 13 additions & 9 deletions src/components/Receive/AddressView.js
Expand Up @@ -15,22 +15,19 @@ import {
hwDeviceInfoSelector,
walletMetaSelector,
} from '../../selectors'
import {showErrorDialog, handleGeneralError} from '../../actions'
import {showErrorDialog} from '../../actions'
import {setLedgerDeviceId, setLedgerDeviceObj} from '../../actions/hwWallet'

import {Text, Modal} from '../UiKit'
import AddressModal from './AddressModal'
import LedgerTransportSwitchModal from '../Ledger/LedgerTransportSwitchModal'
import LedgerConnect from '../Ledger/LedgerConnect'
import AddressVerifyModal from './AddressVerifyModal'
import {
verifyAddress,
GeneralConnectionError,
LedgerUserError,
} from '../../crypto/shelley/ledgerUtils'
import {verifyAddress} from '../../crypto/shelley/ledgerUtils'
import walletManager from '../../crypto/walletManager'
import {formatPath} from '../../crypto/commonUtils'
import {errorMessages} from '../../i18n/global-messages'
import LocalizableError from '../../i18n/LocalizableError'
import {Logger} from '../../utils/logging'
import {CONFIG} from '../../config/config'

Expand Down Expand Up @@ -69,11 +66,18 @@ const _handleOnVerifyAddress = async (
useUSB,
)
} catch (e) {
if (e instanceof GeneralConnectionError || e instanceof LedgerUserError) {
await showErrorDialog(errorMessages.hwConnectionError, intl)
if (e instanceof LocalizableError) {
await showErrorDialog(errorMessages.hwConnectionError, intl, {
message: intl.formatMessage({
id: e.id,
defaultMessage: e.defaultMessage,
}),
})
} else {
Logger.error(e)
handleGeneralError('Could not verify address', e, intl)
await showErrorDialog(errorMessages.hwConnectionError, intl, {
message: String(e.message),
})
}
} finally {
closeDetails()
Expand Down
43 changes: 21 additions & 22 deletions src/components/Send/ConfirmScreen.js
Expand Up @@ -7,7 +7,6 @@ import {ScrollView, View, Platform} from 'react-native'
import {withHandlers, withStateHandlers} from 'recompose'
import {SafeAreaView} from 'react-navigation'
import {injectIntl, defineMessages} from 'react-intl'
import {ErrorCodes} from '@cardano-foundation/ledgerjs-hw-app-cardano'

import {
Text,
Expand All @@ -30,19 +29,10 @@ import globalMessages, {
confirmationMessages,
} from '../../i18n/global-messages'
import walletManager, {SystemAuthDisabled} from '../../crypto/walletManager'
import {
GeneralConnectionError,
LedgerUserError,
} from '../../crypto/shelley/ledgerUtils'
import {SEND_ROUTES, WALLET_ROUTES, WALLET_INIT_ROUTES} from '../../RoutesList'
import {CONFIG} from '../../config/config'
import KeyStore from '../../crypto/KeyStore'
import {
showErrorDialog,
handleGeneralError,
submitTransaction,
submitSignedTx,
} from '../../actions'
import {showErrorDialog, submitTransaction, submitSignedTx} from '../../actions'
import {setLedgerDeviceId, setLedgerDeviceObj} from '../../actions/hwWallet'
import {withNavigationTitle} from '../../utils/renderUtils'
import {formatAdaWithSymbol, formatAdaWithText} from '../../utils/format'
Expand All @@ -52,6 +42,7 @@ import {ignoreConcurrentAsyncHandler} from '../../utils/utils'
import LedgerTransportSwitchModal from '../Ledger/LedgerTransportSwitchModal'
import LedgerConnect from '../Ledger/LedgerConnect'
import HWInstructions from '../Ledger/HWInstructions'
import LocalizableError from '../../i18n/LocalizableError'

import type {BaseSignRequest} from '../../crypto/types'

Expand Down Expand Up @@ -102,7 +93,9 @@ const handleOnConfirm = async (
if (e instanceof NetworkError) {
await showErrorDialog(errorMessages.networkError, intl)
} else if (e instanceof ApiError) {
await showErrorDialog(errorMessages.apiError, intl)
await showErrorDialog(errorMessages.apiError, intl, {
message: JSON.stringify(e.request),
})
} else {
throw e
}
Expand All @@ -119,15 +112,17 @@ const handleOnConfirm = async (
)
await submitTx(Buffer.from(signedTx.encodedTx).toString('base64'))
} catch (e) {
if (e.statusCode === ErrorCodes.ERR_REJECTED_BY_USER) {
return
} else if (
e instanceof GeneralConnectionError ||
e instanceof LedgerUserError
) {
await showErrorDialog(errorMessages.hwConnectionError, intl)
if (e instanceof LocalizableError) {
await showErrorDialog(errorMessages.generalTxError, intl, {
message: intl.formatMessage({
id: e.id,
defaultMessage: e.defaultMessage,
}),
})
} else {
handleGeneralError('Could not submit transaction', e, intl)
await showErrorDialog(errorMessages.generalTxError, intl, {
message: String(e.message),
})
}
}
})
Expand All @@ -154,7 +149,9 @@ const handleOnConfirm = async (

return
} else {
handleGeneralError('Could not submit transaction', e, intl)
await showErrorDialog(errorMessages.generalTxError, intl, {
message: String(e.message),
})
}
}

Expand All @@ -175,7 +172,9 @@ const handleOnConfirm = async (
if (e instanceof WrongPassword) {
await showErrorDialog(errorMessages.incorrectPassword, intl)
} else {
handleGeneralError('Could not submit transaction', e, intl)
await showErrorDialog(errorMessages.generalTxError, intl, {
message: String(e.message),
})
}
}
}
Expand Down
64 changes: 42 additions & 22 deletions src/components/WalletInit/ConnectNanoX/ConnectNanoXScreen.js
Expand Up @@ -11,6 +11,9 @@ import {ProgressStep} from '../../UiKit'
import {withNavigationTitle} from '../../../utils/renderUtils'
import {WALLET_INIT_ROUTES} from '../../../RoutesList'
import {Logger} from '../../../utils/logging'
import {errorMessages} from '../../../i18n/global-messages'
import {showErrorDialog} from '../../../actions'
import LocalizableError from '../../../i18n/LocalizableError'

import styles from './styles/ConnectNanoXScreen.style'

Expand All @@ -35,26 +38,43 @@ const _navigateToSave = async (
deviceId: ?DeviceId,
deviceObj: ?DeviceObj,
navigation: Navigation,
intl: intlShape,
): Promise<void> => {
Logger.debug('deviceId', deviceId)
Logger.debug('deviceObj', deviceObj)
const useUSB = navigation.getParam('useUSB') === true
const networkId = navigation.getParam('networkId')
const walletImplementationId = navigation.getParam('walletImplementationId')
if (deviceId == null && deviceObj == null) {
throw new Error('null descriptor, should never happen')
try {
Logger.debug('deviceId', deviceId)
Logger.debug('deviceObj', deviceObj)
const useUSB = navigation.getParam('useUSB') === true
const networkId = navigation.getParam('networkId')
const walletImplementationId = navigation.getParam('walletImplementationId')
if (deviceId == null && deviceObj == null) {
throw new Error('null descriptor, should never happen')
}
const hwDeviceInfo = await getHWDeviceInfo(
walletImplementationId,
deviceId,
deviceObj,
useUSB,
)
navigation.navigate(WALLET_INIT_ROUTES.SAVE_NANO_X, {
hwDeviceInfo,
networkId,
walletImplementationId,
})
} catch (e) {
if (e instanceof LocalizableError) {
await showErrorDialog(errorMessages.hwConnectionError, intl, {
message: intl.formatMessage({
id: e.id,
defaultMessage: e.defaultMessage,
}),
})
} else {
Logger.error(e)
await showErrorDialog(errorMessages.hwConnectionError, intl, {
message: String(e.message),
})
}
}
const hwDeviceInfo = await getHWDeviceInfo(
walletImplementationId,
deviceId,
deviceObj,
useUSB,
)
navigation.navigate(WALLET_INIT_ROUTES.SAVE_NANO_X, {
hwDeviceInfo,
networkId,
walletImplementationId,
})
}

type Props = {
Expand Down Expand Up @@ -100,11 +120,11 @@ export default injectIntl(
(compose(
withNavigationTitle(({intl}) => intl.formatMessage(messages.title)),
withHandlers({
onConnectBLE: ({navigation}) => async (deviceId: DeviceId) => {
await _navigateToSave(deviceId, null, navigation)
onConnectBLE: ({navigation, intl}) => async (deviceId: DeviceId) => {
await _navigateToSave(deviceId, null, navigation, intl)
},
onConnectUSB: ({navigation}) => async (deviceObj: DeviceObj) => {
await _navigateToSave(null, deviceObj, navigation)
onConnectUSB: ({navigation, intl}) => async (deviceObj: DeviceObj) => {
await _navigateToSave(null, deviceObj, navigation, intl)
},
}),
)(ConnectNanoXScreen): ComponentType<ExternalProps>),
Expand Down
1 change: 1 addition & 0 deletions src/config/config.js
Expand Up @@ -73,6 +73,7 @@ const HARDWARE_WALLETS = {
MODEL: 'Nano',
ENABLE_USB_TRANSPORT: true,
USB_MIN_SDK: 24, // USB transport officially supported for Android SDK >= 24
MIN_FIRMWARE_VERSION: '2.0.4',
},
}

Expand Down

0 comments on commit 0ed84f8

Please sign in to comment.