diff --git a/locales/en.json b/locales/en.json index 2561ce3ce..6c53d961d 100644 --- a/locales/en.json +++ b/locales/en.json @@ -1080,12 +1080,14 @@ "stores.SettingsStore.lndhubSuccess": "Successfully created LNDHub account. Record the username and password somewhere so you can restore your funds if something happens to your device. Then hit Save Wallet Config to continue.", "stores.SettingsStore.lndhubError": "Error creating LNDHub account. Please check the host and try again.", "stores.SettingsStore.lndhubLoginError": "Failed to log in to LNDHub server", + "stores.SettingsStore.lndhubConnectError": "Unable to connect to LNDHub server. Please verify the host address is correct and the service is running.", "stores.SettingsStore.lncConnectError": "Failed to connect the LNC client to the proxy server", "stores.LSPStore.error": "LSP error", "stores.LSPStore.connectionError": "Could not connect to LSP. Please check your LSP settings or try again later.", "stores.LightningAddressStore.preimageNotFound": "Pre-image not found on your device. Did you recently change devices?", "error.connectionRefused": "Host unreachable. Try restarting your node or its Tor process.", "error.hostUnreachable": "Host unreachable. Try restarting your node or its Tor process.", + "error.nodeConnectError": "Unable to connect to node. Please verify the host and port are correct and the service is running.", "error.torBootstrap": "Error starting up Tor on your phone. Try restarting Zeus. If the problem persists consider using the Orbot app to connect to Tor, or using an alternative connection method like Lightning Node Connect or Tailscale.", "error.sendingPayment": "Error sending payment", "error.failureReasonTimeout": "There are more routes to try, but the payment timeout was exceeded.", diff --git a/stores/SettingsStore.ts b/stores/SettingsStore.ts index e66d23ba9..f1dfb5642 100644 --- a/stores/SettingsStore.ts +++ b/stores/SettingsStore.ts @@ -1767,13 +1767,21 @@ export default class SettingsStore { this.refreshToken = data.refresh_token; resolve(data); }) - .catch(() => { - // handle error + .catch((error: any) => { this.loading = false; this.error = true; - this.errorMsg = localeString( - 'stores.SettingsStore.lndhubLoginError' - ); + if ( + typeof error.message === 'string' && + error.message.includes('"bad auth"') + ) { + this.errorMsg = localeString( + 'stores.SettingsStore.lndhubLoginError' + ); + } else { + this.errorMsg = localeString( + 'stores.SettingsStore.lndhubConnectError' + ); + } resolve(); }); }); diff --git a/stores/TransactionsStore.ts b/stores/TransactionsStore.ts index 5072dafc0..cbc58066b 100644 --- a/stores/TransactionsStore.ts +++ b/stores/TransactionsStore.ts @@ -566,7 +566,7 @@ export default class TransactionsStore { const isKeysend = result?.htlcs?.[0]?.route?.hops?.[0]?.custom_records?.[ keySendPreimageType - ]; + ] != null; // TODO add message for in-flight transactions if ( @@ -590,7 +590,6 @@ export default class TransactionsStore { ) : errorToUserFriendly( result.failure_reason, - true, isKeysend ? ['Keysend'] : undefined )) || errorToUserFriendly(result.payment_error); } diff --git a/utils/ErrorUtils.test.ts b/utils/ErrorUtils.test.ts index f669454a7..abbe0bdd6 100644 --- a/utils/ErrorUtils.test.ts +++ b/utils/ErrorUtils.test.ts @@ -1,10 +1,7 @@ import { errorToUserFriendly } from './ErrorUtils'; jest.mock('./LocaleUtils', () => ({ - localeString: (key: string) => { - const EN = require('../locales/en.json'); - return EN[key]; - } + localeString: (key: string) => require('../locales/en.json')[key] })); describe('ErrorUtils', () => { @@ -19,8 +16,7 @@ describe('ErrorUtils', () => { "details": [] }`, name: 'test' - }), - false + }) ) ).toEqual('transaction output is dust'); expect( @@ -32,8 +28,7 @@ describe('ErrorUtils', () => { "details": [] }`, name: 'test' - }), - false + }) ) ).toEqual( 'proto: (line 1:126): invalid value for uint64 type: 0.1' @@ -50,8 +45,7 @@ describe('ErrorUtils', () => { } `, name: 'test' - }), - false + }) ) ).toEqual('invoice is already paid'); expect( @@ -66,8 +60,7 @@ describe('ErrorUtils', () => { } `, name: 'test' - }), - false + }) ) ).toEqual( 'Host unreachable. Try restarting your node or its Tor process.' @@ -78,11 +71,10 @@ describe('ErrorUtils', () => { message: 'Error: called `Result::unwrap()` on an `Err` value: BootStrapError("Timeout waiting for bootstrap")', name: 'test' - }), - false + }) ) ).toEqual( - 'Error starting up Tor on your phone. Try restarting Zeus. If the problem persists consider using the Orbot app to connect to Tor, or using an alternative connection method like Lightning Node Connect or Tailscale.' + 'Error starting up Tor on your phone. Try restarting ZEUS. If the problem persists consider using the Orbot app to connect to Tor, or using an alternative connection method like Lightning Node Connect or Tailscale.' ); expect( errorToUserFriendly( @@ -90,11 +82,24 @@ describe('ErrorUtils', () => { message: 'Error: called `Result::unwrap()` on an `Err` value: BootStrapError("Timeout waiting for boostrap")', name: 'test' - }), - false + }) ) ).toEqual( - 'Error starting up Tor on your phone. Try restarting Zeus. If the problem persists consider using the Orbot app to connect to Tor, or using an alternative connection method like Lightning Node Connect or Tailscale.' + 'Error starting up Tor on your phone. Try restarting ZEUS. If the problem persists consider using the Orbot app to connect to Tor, or using an alternative connection method like Lightning Node Connect or Tailscale.' + ); + }); + + it('Handles partial error message matches', () => { + expect( + errorToUserFriendly( + Object.assign(new Error(), { + message: + 'Error: Failed to connect to /can-be-any-host:8082', + name: 'test' + }) + ) + ).toEqual( + 'Unable to connect to node. Please verify the host and port are correct and the service is running.' ); }); @@ -105,7 +110,6 @@ describe('ErrorUtils', () => { message: 'FAILURE_REASON_INCORRECT_PAYMENT_DETAILS', name: 'test' }), - true, ['UnhandledContext'] ) ).toEqual( @@ -120,7 +124,6 @@ describe('ErrorUtils', () => { message: 'FAILURE_REASON_INCORRECT_PAYMENT_DETAILS', name: 'test' }), - true, ['Keysend'] ) ).toEqual( @@ -134,23 +137,21 @@ describe('ErrorUtils', () => { Object.assign(new Error(), { message: 'Random message', name: 'test' - }), - false + }) ) ).toEqual('Random message'); }); it('Return string if error is sent as a string', () => { - expect( - errorToUserFriendly(new Error('Payment timed out'), false) - ).toEqual('Payment timed out'); + expect(errorToUserFriendly(new Error('Payment timed out'))).toEqual( + 'Payment timed out' + ); }); it('Handles PascalCased LSP error messages', () => { expect( errorToUserFriendly( - new Error('ChannelExpiryBlocksTooHighInCreateOrderRequest'), - false + new Error('ChannelExpiryBlocksTooHighInCreateOrderRequest') ) ).toEqual('Channel expiry blocks too high in create order request'); }); diff --git a/utils/ErrorUtils.ts b/utils/ErrorUtils.ts index a9982216f..8935b1cea 100644 --- a/utils/ErrorUtils.ts +++ b/utils/ErrorUtils.ts @@ -5,6 +5,8 @@ const userFriendlyErrors: any = { 'error.torBootstrap', 'Error: called `Result::unwrap()` on an `Err` value: BootStrapError("Timeout waiting for boostrap")': 'error.torBootstrap', + 'Error: Failed to connect to': 'error.nodeConnectError', + 'Error: Unable to resolve host': 'error.nodeConnectError', FAILURE_REASON_TIMEOUT: 'error.failureReasonTimeout', FAILURE_REASON_NO_ROUTE: 'error.failureReasonNoRoute', FAILURE_REASON_ERROR: 'error.failureReasonError', @@ -17,11 +19,7 @@ const userFriendlyErrors: any = { const pascalCase = /^[A-Z](([a-z0-9]+[A-Z]?)*)$/; -const errorToUserFriendly = ( - error: Error, - localize = true, - errorContext?: string[] -) => { +const errorToUserFriendly = (error: Error, errorContext?: string[]) => { let errorMessage: string = error?.message; let errorObject: any; @@ -48,29 +46,28 @@ const errorToUserFriendly = ( errorMsg = errorMsg.charAt(0).toUpperCase() + errorMsg.slice(1); } - if (localize) { - const localeString = require('./LocaleUtils').localeString; - let baseError = - localeString(userFriendlyErrors[errorMsg])?.replace( - 'Zeus', - 'ZEUS' - ) || errorMsg; + const matchingPattern = Object.keys(userFriendlyErrors).find((pattern) => + errorMsg.includes(pattern) + ); - if ( - errorContext?.includes('Keysend') && - errorMsg === 'FAILURE_REASON_INCORRECT_PAYMENT_DETAILS' - ) { - baseError += - ' ' + - localeString( - 'error.failureReasonIncorrectPaymentDetailsKeysend' - ); - } - return baseError; - } else { - const EN = require('../locales/en.json'); - return EN[userFriendlyErrors[errorMsg]] || errorMsg; + let localeKey = matchingPattern + ? userFriendlyErrors[matchingPattern] + : null; + + const localeString = require('./LocaleUtils').localeString; + let baseError = localeKey + ? localeString(localeKey)?.replace('Zeus', 'ZEUS') + : errorMsg; + + if ( + errorContext?.includes('Keysend') && + errorMsg === 'FAILURE_REASON_INCORRECT_PAYMENT_DETAILS' + ) { + baseError += + ' ' + + localeString('error.failureReasonIncorrectPaymentDetailsKeysend'); } + return baseError; }; export { errorToUserFriendly };