mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-23 13:59:09 +02:00
[client] Return innermost gRPC status from WaitExtendAuthSession
The daemon wrapped the management ExtendAuthSession error through two %v/%w layers and remapped it to codes.Internal, so the UI surfaced the full wrapped chain instead of the root cause. Forward only the innermost gRPC status (original code + clean desc) to the client and log the full chain. gstatus.FromError does not unwrap, so add innermostStatus to walk the %w chain.
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
package server
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
gstatus "google.golang.org/grpc/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInnermostStatus(t *testing.T) {
|
||||||
|
t.Run("wrapped gRPC status", func(t *testing.T) {
|
||||||
|
inner := gstatus.Error(codes.PermissionDenied, "peer is already registered by a different User or a Setup Key")
|
||||||
|
// Mirror the daemon wrap chain: engine wraps with %w, mgm error is the inner status.
|
||||||
|
wrapped := fmt.Errorf("extend auth session on management: %w", inner)
|
||||||
|
|
||||||
|
st := innermostStatus(wrapped)
|
||||||
|
require.NotNil(t, st)
|
||||||
|
require.Equal(t, codes.PermissionDenied, st.Code())
|
||||||
|
require.Equal(t, "peer is already registered by a different User or a Setup Key", st.Message())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("deepest status wins over an outer one", func(t *testing.T) {
|
||||||
|
inner := gstatus.Error(codes.PermissionDenied, "deepest")
|
||||||
|
chain := fmt.Errorf("outer: %w", fmt.Errorf("mid: %w", inner))
|
||||||
|
|
||||||
|
st := innermostStatus(chain)
|
||||||
|
require.NotNil(t, st)
|
||||||
|
require.Equal(t, codes.PermissionDenied, st.Code())
|
||||||
|
require.Equal(t, "deepest", st.Message())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("no status in chain", func(t *testing.T) {
|
||||||
|
require.Nil(t, innermostStatus(errors.New("plain error")))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("nil error", func(t *testing.T) {
|
||||||
|
require.Nil(t, innermostStatus(nil))
|
||||||
|
})
|
||||||
|
}
|
||||||
+21
-1
@@ -1771,7 +1771,14 @@ func (s *Server) WaitExtendAuthSession(
|
|||||||
|
|
||||||
deadline, err := engine.ExtendAuthSession(ctx, tokenInfo.GetTokenToUse())
|
deadline, err := engine.ExtendAuthSession(ctx, tokenInfo.GetTokenToUse())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, gstatus.Errorf(codes.Internal, "management ExtendAuthSession failed: %v", err)
|
// Log the full wrapped chain, but return only the innermost gRPC
|
||||||
|
// status (code + clean desc) so the UI shows the root cause, not
|
||||||
|
// the daemon's wrapping layers.
|
||||||
|
log.Errorf("management ExtendAuthSession failed: %v", err)
|
||||||
|
if st := innermostStatus(err); st != nil {
|
||||||
|
return nil, gstatus.Error(st.Code(), st.Message())
|
||||||
|
}
|
||||||
|
return nil, gstatus.Errorf(codes.Internal, "%v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
resp := &proto.WaitExtendAuthSessionResponse{}
|
resp := &proto.WaitExtendAuthSessionResponse{}
|
||||||
@@ -2392,3 +2399,16 @@ func logoutPeerGone(err error) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// innermostStatus walks the wrap chain and returns the deepest gRPC status,
|
||||||
|
// or nil when none is present. gstatus.FromError does not unwrap, so a status
|
||||||
|
// wrapped with fmt.Errorf %w would otherwise be missed.
|
||||||
|
func innermostStatus(err error) *gstatus.Status {
|
||||||
|
var found *gstatus.Status
|
||||||
|
for e := err; e != nil; e = errors.Unwrap(e) {
|
||||||
|
if s, ok := gstatus.FromError(e); ok {
|
||||||
|
found = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user