fix: improve error handling and funding UX

This commit is contained in:
Sérgio Salgado 2022-04-10 17:07:20 +01:00
parent 0a591658aa
commit d854f0580e

View file

@ -28,6 +28,9 @@ function errorHandle (e) {
throw new E.InsufficientFundsError() throw new E.InsufficientFundsError()
case -18: case -18:
return createWallet() return createWallet()
case -35:
// Wallet is already loaded, just return
return
default: default:
throw e throw e
} }
@ -35,16 +38,19 @@ function errorHandle (e) {
function checkCryptoCode (cryptoCode) { function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'BTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode)) if (cryptoCode !== 'BTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve() return Promise.resolve().then(loadWallet)
} }
function createWallet () { function createWallet () {
return fetch('createwallet', ['wallet']) return fetch('createwallet', ['wallet'])
.catch(errorHandle) .then(loadWallet)
} }
function loadWallet () { function loadWallet () {
return fetch('loadwallet', ['wallet', true]) return fetch('loadwallet', ['wallet', true])
// Catching the error here to suppress error code -35
// This improves UX on the initial wallet load and serves as error sink
// for wallet creation/loading related issues before actual business logic runs
.catch(errorHandle) .catch(errorHandle)
} }