Skip to content

Commit 4a7356f

Browse files
committed
walletrpc: Returns structured errors from FundPsbt
Converts errors likely to be thrown from string based errors to error classes with returned grpc error codes
1 parent 1287328 commit 4a7356f

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

lnrpc/walletrpc/errors.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package walletrpc
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// PSBTError wraps certain errors returned during the construction of partially
8+
// signed bitcoin transactions performed by FundPSBT
9+
type PSBTError struct {
10+
error
11+
}
12+
13+
var _ error = (*PSBTError)(nil)
14+
15+
// ErrUnspentInputNotFound returns an error indicating that a specific unspent
16+
// input in a given list of non-locked UTXOs was not found, and the given
17+
// missing transaction index
18+
func ErrUnspentInputNotFound(idx int) PSBTError {
19+
return PSBTError{
20+
fmt.Errorf("input %d not found in list of non-"+
21+
"locked UTXO", idx),
22+
}
23+
}
24+
25+
// ErrUnableToFund returns an error indicating that the FundPSBT method failed
26+
// along with the returned error message
27+
func ErrUnableToFund(err error) PSBTError {
28+
return PSBTError{
29+
fmt.Errorf("wallet couldn't fund PSBT: %v", err),
30+
}
31+
}

lnrpc/walletrpc/psbt.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/btcsuite/btcutil/psbt"
1212
"github.com/btcsuite/btcwallet/wtxmgr"
1313
"github.com/lightningnetwork/lnd/lnwallet"
14+
"google.golang.org/grpc/codes"
15+
"google.golang.org/grpc/status"
1416
)
1517

1618
const (
@@ -36,8 +38,7 @@ func verifyInputsUnspent(inputs []*wire.TxIn, utxos []*lnwallet.Utxo) error {
3638
}
3739

3840
if !found {
39-
return fmt.Errorf("input %d not found in list of non-"+
40-
"locked UTXO", idx)
41+
return status.Error(codes.NotFound, ErrUnspentInputNotFound(idx).Error())
4142
}
4243
}
4344

lnrpc/walletrpc/walletkit_server.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import (
3434
"github.com/lightningnetwork/lnd/macaroons"
3535
"github.com/lightningnetwork/lnd/sweep"
3636
"google.golang.org/grpc"
37+
"google.golang.org/grpc/codes"
38+
"google.golang.org/grpc/status"
3739
"gopkg.in/macaroon-bakery.v2/bakery"
3840
)
3941

@@ -1119,7 +1121,7 @@ func (w *WalletKit) FundPsbt(_ context.Context,
11191121
packet, feeSatPerKW, account,
11201122
)
11211123
if err != nil {
1122-
return fmt.Errorf("wallet couldn't fund PSBT: %v", err)
1124+
return status.Error(codes.FailedPrecondition, ErrUnableToFund(err).Error())
11231125
}
11241126

11251127
// Make sure we can properly serialize the packet. If this goes

0 commit comments

Comments
 (0)