@@ -17,6 +17,8 @@ import (
1717 "github.com/lightningnetwork/lnd/lnrpc"
1818 "github.com/lightningnetwork/lnd/lntypes"
1919 "github.com/stretchr/testify/require"
20+ "google.golang.org/grpc/codes"
21+ "google.golang.org/grpc/status"
2022)
2123
2224var (
@@ -372,3 +374,39 @@ func testSuccess(ctx *testContext, amt btcutil.Amount, hash lntypes.Hash,
372374
373375 ctx .finish ()
374376}
377+
378+ // TestWrapGrpcError tests grpc error wrapping in the case where a grpc error
379+ // code is present, and when it is absent.
380+ func TestWrapGrpcError (t * testing.T ) {
381+ tests := []struct {
382+ name string
383+ original error
384+ expectedCode codes.Code
385+ }{
386+ {
387+ name : "out of range error" ,
388+ original : status .Error (
389+ codes .OutOfRange , "err string" ,
390+ ),
391+ expectedCode : codes .OutOfRange ,
392+ },
393+ {
394+ name : "no grpc code" ,
395+ original : errors .New ("no error code" ),
396+ expectedCode : codes .Unknown ,
397+ },
398+ }
399+
400+ for _ , testCase := range tests {
401+ testCase := testCase
402+
403+ t .Run (testCase .name , func (t * testing.T ) {
404+ err := wrapGrpcError ("" , testCase .original )
405+ require .Error (t , err , "test only expects errors" )
406+
407+ status , ok := status .FromError (err )
408+ require .True (t , ok , "test expects grpc code" )
409+ require .Equal (t , testCase .expectedCode , status .Code ())
410+ })
411+ }
412+ }
0 commit comments