Skip to content

Commit c124e76

Browse files
committed
feat(cli): better messages when unlock/create fail
This displays better error messages when `xucli unlock`, `xucli create`, or `xucli restore` calls fail due to an xud node not existing or already existing. Instead of the generic `call is not supported by the current state of xud` message being printed, a more helpful message is printed informing the user of the state of xud and which call should be tried instead. This also returns an error when a wallet that is already unlocked is attempted to be unlocked again. Closes #1370.
1 parent 81a8e65 commit c124e76

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

Diff for: lib/Xud.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class Xud extends EventEmitter {
137137
throw new Error('rpc server cannot be disabled when xud is locked');
138138
}
139139
if (this.rpcServer) {
140-
this.rpcServer.grpcInitService.disabled = true;
140+
this.rpcServer.grpcInitService.disable();
141141
}
142142

143143
this.logger.info(`Local nodePubKey is ${nodeKey.pubKey}`);

Diff for: lib/cli/command.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ export const callback = (argv: Arguments, formatOutput?: Function, displayJson?:
3939
console.error(`could not connect to xud at ${argv.rpchost}:${argv.rpcport}, is xud running?`);
4040
}
4141
} else if (error.code === status.UNIMPLEMENTED && error.message.includes('xud is locked')) {
42-
console.error("xud is locked, run 'xucli unlock' or 'xucli create' then try again");
42+
console.error("xud is locked, run 'xucli unlock', 'xucli create', or 'xucli restore' then try again");
43+
} else if (error.code === status.UNIMPLEMENTED && error.message.includes('xud node cannot be created because it already exists')) {
44+
console.error("an xud node already exists, try unlocking it with 'xucli unlock'");
45+
} else if (error.code === status.UNIMPLEMENTED && error.message.includes('xud node cannot be unlocked because it does not exist')) {
46+
console.error("no xud node exists to unlock, try creating one with 'xucli create' or 'xucli restore'");
47+
} else if (error.code === status.UNIMPLEMENTED && error.message.includes('xud init service is disabled')) {
48+
console.error("xud is running and unlocked, try checking its status with 'xucli getinfo'");
4349
} else {
4450
console.error(`${error.name}: ${error.message}`);
4551
}

Diff for: lib/grpc/GrpcInitService.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ import * as xudrpc from '../proto/xudrpc_pb';
55
import getGrpcError from './getGrpcError';
66

77
class GrpcInitService {
8-
public disabled = false;
8+
private disabled = false;
99
private initService?: InitService;
1010

1111
constructor() {}
1212

13-
public setInitService(initService: InitService) {
13+
public setInitService = (initService: InitService) => {
1414
this.initService = initService;
1515
}
1616

17+
/** Disables the grpc initialization service once xud has been intialized. */
18+
public disable = () => {
19+
this.disabled = true;
20+
this.initService = undefined;
21+
}
22+
1723
/**
1824
* Checks whether this service is ready to handle calls and sends an error to the client
1925
* caller if not ready.

Diff for: lib/grpc/getGrpcError.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ const getGrpcError = (err: any) => {
5656
case serviceErrorCodes.PENDING_CALL_CONFLICT:
5757
code = status.RESOURCE_EXHAUSTED;
5858
break;
59-
case serviceErrorCodes.UNIMPLEMENTED:
59+
case serviceErrorCodes.NODE_ALREADY_EXISTS:
60+
case serviceErrorCodes.NODE_DOES_NOT_EXIST:
6061
code = status.UNIMPLEMENTED;
6162
break;
6263
case p2pErrorCodes.POOL_CLOSED:

Diff for: lib/service/InitService.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class InitService extends EventEmitter {
6161
const { password } = args;
6262

6363
if (!this.nodeKeyExists) {
64-
throw errors.UNIMPLEMENTED;
64+
throw errors.NODE_DOES_NOT_EXIST;
6565
}
6666
await this.prepareCall();
6767

@@ -131,7 +131,7 @@ class InitService extends EventEmitter {
131131

132132
private newWalletValidation = (password: string) => {
133133
if (this.nodeKeyExists) {
134-
throw errors.UNIMPLEMENTED;
134+
throw errors.NODE_ALREADY_EXISTS;
135135
}
136136
if (password.length < 8) {
137137
// lnd requires 8+ character passwords, so we must as well

Diff for: lib/service/errors.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const codesPrefix = errorCodesPrefix.SERVICE;
44
const errorCodes = {
55
INVALID_ARGUMENT: codesPrefix.concat('.1'),
66
NOMATCHING_MODE_IS_REQUIRED: codesPrefix.concat('.2'),
7-
UNIMPLEMENTED: codesPrefix.concat('.3'),
7+
NODE_ALREADY_EXISTS: codesPrefix.concat('.3'),
88
PENDING_CALL_CONFLICT: codesPrefix.concat('.4'),
99
OPEN_CHANNEL_FAILURE: codesPrefix.concat('.5'),
10+
NODE_DOES_NOT_EXIST: codesPrefix.concat('.6'),
1011
};
1112

1213
const errors = {
@@ -18,9 +19,9 @@ const errors = {
1819
message: 'nomatching mode is required',
1920
code: errorCodes.NOMATCHING_MODE_IS_REQUIRED,
2021
}),
21-
UNIMPLEMENTED: {
22-
message: 'call is not supported by the current state of xud',
23-
code: errorCodes.UNIMPLEMENTED,
22+
NODE_ALREADY_EXISTS: {
23+
message: 'xud node cannot be created because it already exists',
24+
code: errorCodes.NODE_ALREADY_EXISTS,
2425
},
2526
PENDING_CALL_CONFLICT: {
2627
message: 'a pending call is ongoing that conflicts with this call',
@@ -30,6 +31,10 @@ const errors = {
3031
message: `failed to open channel with nodePubKey: ${nodePubKey}, currency: ${currency}, amount: ${amount}, message: ${message}`,
3132
code: errorCodes.OPEN_CHANNEL_FAILURE,
3233
}),
34+
NODE_DOES_NOT_EXIST: {
35+
message: 'xud node cannot be unlocked because it does not exist',
36+
code: errorCodes.NODE_DOES_NOT_EXIST,
37+
},
3338
};
3439

3540
export { errorCodes };

0 commit comments

Comments
 (0)