Skip to content

Commit 2d61482

Browse files
committed
cmd: fix error parsed from status
The `codes.Unimplemented` is only returned when the RPCs are not built. When the wallet is in an unexpected state, `codes.Unknown` is returned instead, so we need to catch it properly to make sure we return the right error msg.
1 parent 6531d45 commit 2d61482

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

cmd/commands/commands.go

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -251,41 +251,47 @@ func printModifiedProtoJSON(resp proto.Message) {
251251
// to command actions.
252252
func actionDecorator(f func(*cli.Context) error) func(*cli.Context) error {
253253
return func(c *cli.Context) error {
254-
if err := f(c); err != nil {
255-
s, ok := status.FromError(err)
256-
257-
// If it's a command for the UnlockerService (like
258-
// 'create' or 'unlock') but the wallet is already
259-
// unlocked, then these methods aren't recognized any
260-
// more because this service is shut down after
261-
// successful unlock. That's why the code
262-
// 'Unimplemented' means something different for these
263-
// two commands.
264-
if s.Code() == codes.Unimplemented &&
265-
(c.Command.Name == "create" ||
266-
c.Command.Name == "unlock" ||
267-
c.Command.Name == "changepassword" ||
268-
c.Command.Name == "createwatchonly") {
269-
270-
return fmt.Errorf("Wallet is already unlocked")
271-
}
254+
err := f(c)
272255

273-
// lnd might be active, but not possible to contact
274-
// using RPC if the wallet is encrypted. If we get
275-
// error code Unimplemented, it means that lnd is
276-
// running, but the RPC server is not active yet (only
277-
// WalletUnlocker server active) and most likely this
278-
// is because of an encrypted wallet.
279-
if ok && s.Code() == codes.Unimplemented {
280-
return fmt.Errorf("Wallet is encrypted. " +
281-
"Please unlock using 'lncli unlock', " +
282-
"or set password using 'lncli create'" +
283-
" if this is the first time starting " +
284-
"lnd.")
285-
}
256+
// Exit early if there's no error.
257+
if err == nil {
258+
return nil
259+
}
260+
261+
// Try to parse the Status representatio from this error.
262+
s, ok := status.FromError(err)
263+
264+
// If this cannot be represented by a Status, exit early.
265+
if !ok {
286266
return err
287267
}
288-
return nil
268+
269+
// If it's a command for the UnlockerService (like 'create' or
270+
// 'unlock') but the wallet is already unlocked, then these
271+
// methods aren't recognized any more because this service is
272+
// shut down after successful unlock.
273+
if s.Code() == codes.Unknown &&
274+
(c.Command.Name == "create" ||
275+
c.Command.Name == "unlock" ||
276+
c.Command.Name == "changepassword" ||
277+
c.Command.Name == "createwatchonly") {
278+
279+
return errors.New("wallet is already unlocked")
280+
}
281+
282+
// lnd might be active, but not possible to contact using RPC if
283+
// the wallet is encrypted. If we get error code Unknown, it
284+
// means that lnd is running, but the RPC server is not active
285+
// yet (only WalletUnlocker server active) and most likely this
286+
// is because of an encrypted wallet.
287+
if s.Code() == codes.Unknown {
288+
return errors.New("wallet is encrypted - please " +
289+
"unlock using 'lncli unlock', or set " +
290+
"password using 'lncli create' if this is " +
291+
"the first time starting lnd")
292+
}
293+
294+
return s.Err()
289295
}
290296
}
291297

0 commit comments

Comments
 (0)