diff --git a/src/helpers/deviceAccess.js b/src/helpers/deviceAccess.js index c380fe0c..f6fbc478 100644 --- a/src/helpers/deviceAccess.js +++ b/src/helpers/deviceAccess.js @@ -4,6 +4,7 @@ import type Transport from '@ledgerhq/hw-transport' import TransportNodeHid from '@ledgerhq/hw-transport-node-hid' import { DEBUG_DEVICE } from 'config/constants' import { retry } from './promise' +import { createCustomErrorClass } from './errors' // all open to device must use openDevice so we can prevent race conditions // and guarantee we do one device access at a time. It also will handle the .close() @@ -13,6 +14,16 @@ type WithDevice = (devicePath: string) => (job: (Transport<*>) => Promise) const semaphorePerDevice = {} +const DisconnectedDevice = createCustomErrorClass('DisconnectedDevice') + +const remapError = (p: Promise): Promise => + p.catch(e => { + if (e && e.message && e.message.indexOf('HID') >= 0) { + throw new DisconnectedDevice(e.message) + } + throw e + }) + export const withDevice: WithDevice = devicePath => { const sem = semaphorePerDevice[devicePath] || (semaphorePerDevice[devicePath] = createSemaphore(1)) @@ -23,7 +34,7 @@ export const withDevice: WithDevice = devicePath => { if (DEBUG_DEVICE) t.setDebugMode(true) try { - const res = await job(t) + const res = await remapError(job(t)) // $FlowFixMe return res } finally { diff --git a/static/i18n/en/errors.yml b/static/i18n/en/errors.yml index dc07a20a..01564ee9 100644 --- a/static/i18n/en/errors.yml +++ b/static/i18n/en/errors.yml @@ -1,33 +1,32 @@ -generic: Oops, an unknown error occurred. Please try again or contact Ledger Support. -RangeError: '{{message}}' -Error: '{{message}}' -LedgerAPIErrorWithMessage: '{{message}}' -TransportStatusError: '{{message}}' -TimeoutError: 'The request timed out.' -FeeEstimationFailed: 'Fee estimation error. Try again or set a custom fee (status: {{status}})' -NotEnoughBalance: 'Insufficient funds to proceed.' BtcUnmatchedApp: 'Open the ‘{{currencyName}}’ app on your Ledger device to proceed.' -WrongAppOpened: 'Open the ‘{{currencyName}}’ app on your Ledger device to proceed.' -WrongDeviceForAccount: 'Use the device associated with the account ‘{{accountName}}’.' -LedgerAPINotAvailable: 'Ledger API not available for {{currencyName}}.' -LedgerAPIError: 'Ledger API error. Try again. (HTTP {{status}})' -NetworkDown: 'Your internet connection seems down.' -NoAddressesFound: 'No accounts were found.' -UserRefusedOnDevice: Transaction refused on device. -WebsocketConnectionError: Oops, device connection failed. Please try again. [web-err] -WebsocketConnectionFailed: Oops, device connection failed. Please try again. [web-fail] +DeviceNotGenuine: Device is not genuine DeviceSocketFail: Oops, device connection failed. Please try again. [device-fail] DeviceSocketNoBulkStatus: Oops, device connection failed. Please try again [bulk]. DeviceSocketNoHandler: Oops, device connection failed (handler {{query}}). Please try again. -LatestMCUInstalledError: MCU on device already up to date. +DisconnectedDevice: 'The device was disconnected.' +Error: '{{message}}' +FeeEstimationFailed: 'Fee estimation error. Try again or set a custom fee (status: {{status}})' +generic: Oops, an unknown error occurred. Please try again or contact Ledger Support. HardResetFail: Reset failed. Please try again. - +LatestMCUInstalledError: MCU on device already up to date. +LedgerAPIError: 'Ledger API error. Try again. (HTTP {{status}})' +LedgerAPIErrorWithMessage: '{{message}}' +LedgerAPINotAvailable: 'Ledger API not available for {{currencyName}}.' ManagerAPIsFail: Services are unavailable. Please try again. -ManagerUnexpected: Unexpected error occurred ({{msg}}). Please try again. -ManagerNotEnoughSpace: Not enough storage on device. Uninstall some apps and try again. -ManagerDeviceLocked: Device is locked ManagerAppAlreadyInstalled: App is already installed ManagerAppRelyOnBTC: You must install Bitcoin application first +ManagerDeviceLocked: Device is locked +ManagerNotEnoughSpace: Not enough storage on device. Uninstall some apps and try again. +ManagerUnexpected: Unexpected error occurred ({{msg}}). Please try again. ManagerUninstallBTCDep: You must uninstall other altcoins first - -DeviceNotGenuine: Device is not genuine +NetworkDown: 'Your internet connection seems down.' +NoAddressesFound: 'No accounts were found.' +NotEnoughBalance: 'Insufficient funds to proceed.' +RangeError: '{{message}}' +TimeoutError: 'The request timed out.' +TransportStatusError: '{{message}}' +UserRefusedOnDevice: Transaction refused on device. +WebsocketConnectionError: Oops, device connection failed. Please try again. [web-err] +WebsocketConnectionFailed: Oops, device connection failed. Please try again. [web-fail] +WrongAppOpened: 'Open the ‘{{currencyName}}’ app on your Ledger device to proceed.' +WrongDeviceForAccount: 'Use the device associated with the account ‘{{accountName}}’.'