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.
This commit is contained in:
yyforyongyu
2025-03-17 22:13:11 +08:00
parent 6531d45050
commit 2d6148291f

View File

@@ -251,41 +251,47 @@ func printModifiedProtoJSON(resp proto.Message) {
// to command actions. // to command actions.
func actionDecorator(f func(*cli.Context) error) func(*cli.Context) error { func actionDecorator(f func(*cli.Context) error) func(*cli.Context) error {
return func(c *cli.Context) error { return func(c *cli.Context) error {
if err := f(c); err != nil { err := f(c)
// Exit early if there's no error.
if err == nil {
return nil
}
// Try to parse the Status representatio from this error.
s, ok := status.FromError(err) s, ok := status.FromError(err)
// If it's a command for the UnlockerService (like // If this cannot be represented by a Status, exit early.
// 'create' or 'unlock') but the wallet is already if !ok {
// unlocked, then these methods aren't recognized any return err
// more because this service is shut down after }
// successful unlock. That's why the code
// 'Unimplemented' means something different for these // If it's a command for the UnlockerService (like 'create' or
// two commands. // 'unlock') but the wallet is already unlocked, then these
if s.Code() == codes.Unimplemented && // methods aren't recognized any more because this service is
// shut down after successful unlock.
if s.Code() == codes.Unknown &&
(c.Command.Name == "create" || (c.Command.Name == "create" ||
c.Command.Name == "unlock" || c.Command.Name == "unlock" ||
c.Command.Name == "changepassword" || c.Command.Name == "changepassword" ||
c.Command.Name == "createwatchonly") { c.Command.Name == "createwatchonly") {
return fmt.Errorf("Wallet is already unlocked") return errors.New("wallet is already unlocked")
} }
// lnd might be active, but not possible to contact // lnd might be active, but not possible to contact using RPC if
// using RPC if the wallet is encrypted. If we get // the wallet is encrypted. If we get error code Unknown, it
// error code Unimplemented, it means that lnd is // means that lnd is running, but the RPC server is not active
// running, but the RPC server is not active yet (only // yet (only WalletUnlocker server active) and most likely this
// WalletUnlocker server active) and most likely this
// is because of an encrypted wallet. // is because of an encrypted wallet.
if ok && s.Code() == codes.Unimplemented { if s.Code() == codes.Unknown {
return fmt.Errorf("Wallet is encrypted. " + return errors.New("wallet is encrypted - please " +
"Please unlock using 'lncli unlock', " + "unlock using 'lncli unlock', or set " +
"or set password using 'lncli create'" + "password using 'lncli create' if this is " +
" if this is the first time starting " + "the first time starting lnd")
"lnd.")
} }
return err
} return s.Err()
return nil
} }
} }