Skip to content

Commit c292acf

Browse files
authored
Merge pull request #9605 from yyforyongyu/fix-unlock-wallet
cmd: fix error parsed from status
2 parents 3e8339c + e62aa7d commit c292acf

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-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

docs/release-notes/release-notes-0.19.0.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@
236236

237237
## lncli Updates
238238

239+
* [Fixed](https://github.com/lightningnetwork/lnd/pull/9605) a case where
240+
inaccurate error message is displayed. Previously, when the `lnd` is built
241+
without with a given RPC service yet the `cli` does, running a command to
242+
access the RPC server would give an error saying the wallet is encrypted. This
243+
is now fixed to show specifically which RPC server is missing.
244+
239245
## Code Health
240246

241247
* [Add retry logic](https://github.com/lightningnetwork/lnd/pull/8381) for

0 commit comments

Comments
 (0)