Skip to content

Commit e866c66

Browse files
authored
feat: display disconnection errors and dismiss them on getting connec… (#1019)
* feat: display disconnection errors and dismiss them on getting connected. UI will display the alerts of disconnection from neuron-wallet, and clear them once UI find that connection is built. There may be some status conflicts between UI and wallet, but it's acceptable. * test: fix the e2e test of notification
1 parent f17c701 commit e866c66

5 files changed

Lines changed: 53 additions & 80 deletions

File tree

packages/neuron-ui/src/containers/Main/hooks.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
Command as CommandSubject,
2222
} from 'services/subjects'
2323
import { ckbCore, getTipBlockNumber, getBlockchainInfo } from 'services/chain'
24-
import { ConnectionStatus } from 'utils/const'
24+
import { ConnectionStatus, ErrorCode } from 'utils/const'
2525
import {
2626
networks as networksCache,
2727
currentNetworkID as currentNetworkIDCache,
@@ -40,6 +40,10 @@ export const useSyncChainData = ({ chainURL, dispatch }: { chainURL: string; dis
4040
type: AppActions.UpdateTipBlockNumber,
4141
payload: BigInt(tipBlockNumber).toString(),
4242
})
43+
dispatch({
44+
type: AppActions.ClearNotificationsOfCode,
45+
payload: ErrorCode.NodeDisconnected,
46+
})
4347
})
4448
.catch((err: Error) => {
4549
if (process.env.NODE_ENV === 'development') {

packages/neuron-ui/src/states/stateProvider/actionCreators/app.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ export const addPopup = (text: string) => (dispatch: StateDispatch) => {
7676
}
7777

7878
export const addNotification = (message: State.Message<ErrorCode>) => (dispatch: StateDispatch) => {
79-
if (message && message.code === ErrorCode.NodeDisconnected) {
80-
return
81-
}
8279
dispatch({
8380
type: AppActions.AddNotification,
8481
payload: message,

packages/neuron-wallet/src/controllers/wallets.ts

Lines changed: 37 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
import i18n from 'utils/i18n'
2121
import AddressService from 'services/addresses'
2222
import WalletCreatedSubject from 'models/subjects/wallet-created-subject'
23-
import logger from 'utils/logger'
2423
import { TransactionWithoutHash } from 'types/cell-types';
2524

2625
export default class WalletsController {
@@ -368,26 +367,18 @@ export default class WalletsController {
368367
if (!params.fee || params.fee === '0') {
369368
feeRate = '1000'
370369
}
371-
try {
372-
const walletsService = WalletsService.getInstance()
373-
const hash = await walletsService.sendCapacity(
374-
params.walletID,
375-
params.items,
376-
params.password,
377-
params.fee,
378-
feeRate,
379-
params.description
380-
)
381-
return {
382-
status: ResponseCode.Success,
383-
result: hash,
384-
}
385-
} catch (err) {
386-
logger.error(`sendCapacity:`, err)
387-
return {
388-
status: err.code || ResponseCode.Fail,
389-
message: `Error: "${err.message}"`,
390-
}
370+
const walletsService = WalletsService.getInstance()
371+
const hash = await walletsService.sendCapacity(
372+
params.walletID,
373+
params.items,
374+
params.password,
375+
params.fee,
376+
feeRate,
377+
params.description
378+
)
379+
return {
380+
status: ResponseCode.Success,
381+
result: hash,
391382
}
392383
}
393384

@@ -401,24 +392,16 @@ export default class WalletsController {
401392
if (!params) {
402393
throw new IsRequired('Parameters')
403394
}
404-
try {
405-
const walletsService = WalletsService.getInstance()
406-
const hash = await walletsService.sendTx(
407-
params.walletID,
408-
params.tx,
409-
params.password,
410-
params.description
411-
)
412-
return {
413-
status: ResponseCode.Success,
414-
result: hash,
415-
}
416-
} catch (err) {
417-
logger.error(`sendTx:`, err)
418-
return {
419-
status: err.code || ResponseCode.Fail,
420-
message: `Error: "${err.message}"`,
421-
}
395+
const walletsService = WalletsService.getInstance()
396+
const hash = await walletsService.sendTx(
397+
params.walletID,
398+
params.tx,
399+
params.password,
400+
params.description
401+
)
402+
return {
403+
status: ResponseCode.Success,
404+
result: hash,
422405
}
423406
}
424407

@@ -435,24 +418,16 @@ export default class WalletsController {
435418
if (!params) {
436419
throw new IsRequired('Parameters')
437420
}
438-
try {
439-
const walletsService = WalletsService.getInstance()
440-
const tx = await walletsService.generateTx(
441-
params.walletID,
442-
params.items,
443-
params.fee,
444-
params.feeRate,
445-
)
446-
return {
447-
status: ResponseCode.Success,
448-
result: tx,
449-
}
450-
} catch (err) {
451-
logger.error(`generateTx:`, err)
452-
return {
453-
status: err.code || ResponseCode.Fail,
454-
message: `Error: "${err.message}"`,
455-
}
421+
const walletsService = WalletsService.getInstance()
422+
const tx = await walletsService.generateTx(
423+
params.walletID,
424+
params.items,
425+
params.fee,
426+
params.feeRate,
427+
)
428+
return {
429+
status: ResponseCode.Success,
430+
result: tx,
456431
}
457432
}
458433

@@ -461,18 +436,11 @@ export default class WalletsController {
461436
if (!params) {
462437
throw new IsRequired('Parameters')
463438
}
464-
try {
465-
const walletsService = WalletsService.getInstance()
466-
const cycles = await walletsService.computeCycles(params.walletID, params.capacities)
467-
return {
468-
status: ResponseCode.Success,
469-
result: cycles,
470-
}
471-
} catch (err) {
472-
return {
473-
status: ResponseCode.Fail,
474-
message: `Error: "${err.message}"`,
475-
}
439+
const walletsService = WalletsService.getInstance()
440+
const cycles = await walletsService.computeCycles(params.walletID, params.capacities)
441+
return {
442+
status: ResponseCode.Success,
443+
result: cycles,
476444
}
477445
}
478446

packages/neuron-wallet/src/decorators/errors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import logger from 'utils/logger'
33

44
const NODE_DISCONNECTED_CODE = 104
55

6-
export const CatchControllerError = (_target: any, _name: string, descriptor: PropertyDescriptor) => {
6+
export const CatchControllerError = (target: any, name: string, descriptor: PropertyDescriptor) => {
77
const originalMethod = descriptor.value
88
return {
99
...descriptor,
1010
async value(...args: any[]) {
1111
try {
1212
return await originalMethod(...args)
1313
} catch (err) {
14-
logger.error(`CatchControllerError:`, err)
14+
logger.error(`${target.name}.${name}:`, err)
1515
if (err.code === 'ECONNREFUSED') {
1616
err.code = NODE_DISCONNECTED_CODE
1717
}

packages/neuron-wallet/tests-e2e/tests/notification.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Application from '../application'
22
import { createWallet } from '../operations'
33

44
/**
5-
* 1. check the alert, it should be empty
5+
* 1. check the alert, it should be disconnected to the network
66
* 2. navigate to wallet settingsState
77
* 3. delete a wallet
88
* 4. input a wrong password
@@ -32,14 +32,15 @@ export default (app: Application) => {
3232

3333
describe('Test alert message and notification', () => {
3434
const messages = {
35+
disconnected: 'Fail to connect to the node',
3536
incorrectPassword: 'Password is incorrect',
3637
}
3738

38-
app.test('There is no alerts after the app launched', async () => {
39+
app.test('It should have an alert message of disconnection', async () => {
3940
const { client } = app.spectron
4041
const alertComponent = await client.$('.ms-MessageBar-text')
4142
const msg = await client.elementIdText(alertComponent.value.ELEMENT)
42-
expect(msg.value).toBe('')
43+
expect(msg.value).toBe(messages.disconnected)
4344
})
4445

4546
app.test('It should have an alert message of incorrect password', async () => {
@@ -56,12 +57,14 @@ export default (app: Application) => {
5657
expect(msg.value).toBe(messages.incorrectPassword)
5758
})
5859

59-
app.test('It should have a message in the notification', async () => {
60+
app.test('It should have two messages in the notification', async () => {
6061
const { client } = app.spectron
6162
const messageComponents = await client.$$('.ms-Panel-content p')
6263
expect(messageComponents.length).toBe(Object.keys(messages).length)
6364
const incorrectPasswordMsg = await client.element(`//P[text()="${messages.incorrectPassword}"]`)
65+
const disconnectMsg = await client.element(`//P[text()="${messages.disconnected}"]`)
6466
expect(incorrectPasswordMsg.state).not.toBe('failure')
67+
expect(disconnectMsg.state).not.toBe('failed')
6568
})
6669

6770
// TODO: dismiss a message
@@ -76,7 +79,8 @@ export default (app: Application) => {
7679
await app.waitUntilLoaded()
7780
app.wait(4000)
7881
const alertComponent = await client.$('.ms-MessageBar--error')
79-
expect(alertComponent.state).toBe('failure')
82+
const msg = await client.elementIdText(alertComponent.value.ELEMENT)
83+
expect(msg.value).toBe(messages.disconnected)
8084
})
8185
})
8286
}

0 commit comments

Comments
 (0)