Skip to content

Commit

Permalink
fix: improve error wrap. (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
mo3et authored Dec 13, 2024
1 parent 999cc77 commit e498f36
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions db/mongoutil/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func Check(ctx context.Context, config *Config) error {
// ValidateAndSetDefaults validates the configuration and sets default values.
func (c *Config) ValidateAndSetDefaults() error {
if c.Uri == "" && len(c.Address) == 0 {
return errs.New("either Uri or Address must be provided")
return errs.Wrap(errs.New("either Uri or Address must be provided"))
}
if c.Database == "" {
return errs.New("database is required")
return errs.Wrap(errs.New("database is required"))
}
if c.MaxPoolSize <= 0 {
c.MaxPoolSize = defaultMaxPoolSize
Expand Down
16 changes: 9 additions & 7 deletions discovery/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"time"

"github.com/openimsdk/tools/errs"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
v1 "k8s.io/api/core/v1"
Expand All @@ -34,12 +35,13 @@ type KubernetesConnManager struct {
func NewKubernetesConnManager(namespace string, options ...grpc.DialOption) (*KubernetesConnManager, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("failed to create in-cluster config: %v", err)
return nil, errs.WrapMsg(err, "failed to create in-cluster config:")
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("failed to create clientset: %v", err)
return nil, errs.WrapMsg(err, "failed to create clientset:")

}

k := &KubernetesConnManager{
Expand All @@ -57,12 +59,12 @@ func NewKubernetesConnManager(namespace string, options ...grpc.DialOption) (*Ku
func (k *KubernetesConnManager) initializeConns(serviceName string) error {
port, err := k.getServicePort(serviceName)
if err != nil {
return err
return errs.Wrap(err)
}

endpoints, err := k.clientset.CoreV1().Endpoints(k.namespace).Get(context.Background(), serviceName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get endpoints for service %s: %v", serviceName, err)
return errs.WrapMsg(err, "failed to get endpoints", "serviceName", serviceName)
}

// fmt.Println("Endpoints:", endpoints, "endpoints.Subsets:", endpoints.Subsets)
Expand All @@ -74,7 +76,7 @@ func (k *KubernetesConnManager) initializeConns(serviceName string) error {
// fmt.Println("IP target:", target)
conn, err := grpc.Dial(target, append(k.dialOptions, grpc.WithTransportCredentials(insecure.NewCredentials()))...)
if err != nil {
return fmt.Errorf("failed to dial endpoint %s: %v", target, err)
return errs.WrapMsg(err, "failed to dial endpoint", "target", target)
}
conns = append(conns, conn)
}
Expand Down Expand Up @@ -106,8 +108,8 @@ func (k *KubernetesConnManager) GetConns(ctx context.Context, serviceName string
k.mu.Unlock()

if err := k.initializeConns(serviceName); err != nil {
fmt.Println("Failed to initialize connections:", err)
return nil, fmt.Errorf("failed to initialize connections for service %s: %v", serviceName, err)

return nil, errs.WrapMsg(err, "Failed to initialize connections for service", "serviceName", serviceName)
}

return k.connMap[serviceName], nil
Expand Down

0 comments on commit e498f36

Please sign in to comment.