Skip to content

Commit

Permalink
update errors handling in pkg/registry
Browse files Browse the repository at this point in the history
Signed-off-by: Ruslan Bayandinov <wazsone@ya.ru>
  • Loading branch information
wazsone committed Feb 1, 2023
1 parent 2d18192 commit a00e6a2
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 54 deletions.
5 changes: 3 additions & 2 deletions pkg/registry/common/authorize/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -20,6 +20,7 @@ import (
"context"

"github.com/golang-jwt/jwt/v4"
"github.com/pkg/errors"
"github.com/spiffe/go-spiffe/v2/spiffeid"

"github.com/networkservicemesh/sdk/pkg/registry/common/grpcmetadata"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (l *policiesList) check(ctx context.Context, input RegistryOpaInput) error

if err := policy.Check(ctx, input); err != nil {
log.FromContext(ctx).Infof("policy failed %v", policy)
return err
return errors.Wrap(err, "an error occurred during authorization policy check")
}

log.FromContext(ctx).Infof("policy passed")
Expand Down
9 changes: 5 additions & 4 deletions pkg/registry/common/connect/ns_client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022 Cisco and/or its affiliates.
// Copyright (c) 2021-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -21,6 +21,7 @@ import (

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/registry"
"github.com/pkg/errors"
"google.golang.org/grpc"

"github.com/networkservicemesh/sdk/pkg/registry/common/clientconn"
Expand All @@ -31,23 +32,23 @@ type connectNSClient struct{}
func (n *connectNSClient) Register(ctx context.Context, in *registry.NetworkService, opts ...grpc.CallOption) (*registry.NetworkService, error) {
cc, loaded := clientconn.Load(ctx)
if !loaded {
return nil, errNoCCProvided
return nil, errors.New(errNoCCProvidedMsg)
}
return registry.NewNetworkServiceRegistryClient(cc).Register(ctx, in, opts...)
}

func (n *connectNSClient) Find(ctx context.Context, in *registry.NetworkServiceQuery, opts ...grpc.CallOption) (registry.NetworkServiceRegistry_FindClient, error) {
cc, loaded := clientconn.Load(ctx)
if !loaded {
return nil, errNoCCProvided
return nil, errors.New(errNoCCProvidedMsg)
}
return registry.NewNetworkServiceRegistryClient(cc).Find(ctx, in, opts...)
}

func (n *connectNSClient) Unregister(ctx context.Context, in *registry.NetworkService, opts ...grpc.CallOption) (*empty.Empty, error) {
cc, loaded := clientconn.Load(ctx)
if !loaded {
return nil, errNoCCProvided
return nil, errors.New(errNoCCProvidedMsg)
}
return registry.NewNetworkServiceRegistryClient(cc).Unregister(ctx, in, opts...)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/common/connect/ns_server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022 Cisco and/or its affiliates.
// Copyright (c) 2021-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -61,7 +61,7 @@ func (c *connectNSServer) Find(query *registry.NetworkServiceQuery, server regis

for resp := range registry.ReadNetworkServiceChannel(clientResp) {
if err := server.Send(resp); err != nil {
return err
return errors.Wrapf(err, "NetworkServiceRegistry find server failed to send a response %s", resp.String())
}
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/registry/common/connect/nse_client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022 Cisco and/or its affiliates.
// Copyright (c) 2021-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -27,30 +27,30 @@ import (
"github.com/networkservicemesh/sdk/pkg/registry/common/clientconn"
)

var errNoCCProvided = errors.New("no grpc.ClientConnInterface provided")
const errNoCCProvidedMsg = "no grpc.ClientConnInterface provided"

type connectNSEClient struct{}

func (n *connectNSEClient) Register(ctx context.Context, in *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*registry.NetworkServiceEndpoint, error) {
cc, loaded := clientconn.Load(ctx)
if !loaded {
return nil, errNoCCProvided
return nil, errors.New(errNoCCProvidedMsg)
}
return registry.NewNetworkServiceEndpointRegistryClient(cc).Register(ctx, in, opts...)
}

func (n *connectNSEClient) Find(ctx context.Context, in *registry.NetworkServiceEndpointQuery, opts ...grpc.CallOption) (registry.NetworkServiceEndpointRegistry_FindClient, error) {
cc, loaded := clientconn.Load(ctx)
if !loaded {
return nil, errNoCCProvided
return nil, errors.New(errNoCCProvidedMsg)
}
return registry.NewNetworkServiceEndpointRegistryClient(cc).Find(ctx, in, opts...)
}

func (n *connectNSEClient) Unregister(ctx context.Context, in *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*empty.Empty, error) {
cc, loaded := clientconn.Load(ctx)
if !loaded {
return nil, errNoCCProvided
return nil, errors.New(errNoCCProvidedMsg)
}
return registry.NewNetworkServiceEndpointRegistryClient(cc).Unregister(ctx, in, opts...)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/common/connect/nse_server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022 Cisco and/or its affiliates.
// Copyright (c) 2021-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -61,7 +61,7 @@ func (c *connectNSEServer) Find(query *registry.NetworkServiceEndpointQuery, ser

for resp := range registry.ReadNetworkServiceEndpointChannel(clientResp) {
if err := server.Send(resp); err != nil {
return err
return errors.Wrapf(err, "NetworkServiceEndpointRegistry find server failed to send a response %s", resp.String())
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/common/dnsresolve/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func resolveDomain(ctx context.Context, service, domain string, r Resolver) (u *

_, records, err := r.LookupSRV(ctx, "", "", serviceDomain)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrapf(err, "failed to resolve a SRV query for a %s", serviceDomain)
}
if len(records) == 0 {
return nil, errors.New("resolver.LookupSERV return empty result")
Expand All @@ -89,7 +89,7 @@ func resolveDomain(ctx context.Context, service, domain string, r Resolver) (u *

ips, err := r.LookupIPAddr(ctx, serviceDomain)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrapf(err, "failed to look up a host using a local resolver for a %s", serviceDomain)
}
if len(ips) == 0 {
return nil, errors.New("resolver.LookupIPAddr return empty result")
Expand All @@ -114,7 +114,7 @@ func formatURL(ip, port string) (*url.URL, error) {

parsedURL, err := url.Parse(urlStr)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrapf(err, "failed to parse url %s", urlStr)
}
return parsedURL, nil
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/registry/common/dnsresolve/ns_server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -71,7 +73,7 @@ func (d *dnsNSResolveServer) Register(ctx context.Context, ns *registry.NetworkS

resp.Name = interdomain.Join(resp.Name, domain)

return resp, err
return resp, nil
}

type dnsFindNSServer struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/common/dnsresolve/nse_server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022 Cisco Systems, Inc.
// Copyright (c) 2022-2023 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -123,7 +123,7 @@ func (d *dnsNSEResolveServer) Register(ctx context.Context, nse *registry.Networ
return interdomain.Join(s, domain)
})

return resp, err
return resp, nil
}

type dnsFindNSEServer struct {
Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/common/grpcmetadata/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func fromMD(md metadata.MD) (*Path, error) {
if len(pathValue) > 0 {
err := json.Unmarshal([]byte(pathValue[len(pathValue)-1]), path)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrap(err, "failed to parse a JSON-encoded data and store the result")
}
}

Expand All @@ -77,7 +77,7 @@ func fromContext(ctx context.Context) (*Path, error) {
func appendToMetadata(ctx context.Context, path *Path) (context.Context, error) {
bytes, err := json.Marshal(path)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrap(err, "failed to convert a provided path into JSON")
}
ctx = metadata.AppendToOutgoingContext(ctx, pathKey, string(bytes))
return ctx, nil
Expand All @@ -86,7 +86,7 @@ func appendToMetadata(ctx context.Context, path *Path) (context.Context, error)
func sendPath(ctx context.Context, path *Path) error {
bytes, err := json.Marshal(path)
if err != nil {
return errors.WithStack(err)
return errors.Wrap(err, "failed to convert a provided path into JSON")
}

header := metadata.Pairs(pathKey, string(bytes))
Expand Down
5 changes: 3 additions & 2 deletions pkg/registry/common/interdomainbypass/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ func NewNetworkServiceEndpointRegistryServer(m *stringurl.Map, u *url.URL) regis
}

func (s *interdomainBypassNSEFindServer) Send(nseResp *registry.NetworkServiceEndpointResponse) error {
u, err := url.Parse(nseResp.GetNetworkServiceEndpoint().GetUrl())
nseURL := nseResp.GetNetworkServiceEndpoint().GetUrl()
u, err := url.Parse(nseURL)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to parse url %s", nseURL)
}
s.m.LoadOrStore(nseResp.NetworkServiceEndpoint.GetName(), u)
nseResp.GetNetworkServiceEndpoint().Url = s.u.String()
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/common/memory/ns_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (s *memoryNSServer) Find(query *registry.NetworkServiceQuery, server regist
}

if err := server.Send(nsResp); err != nil {
return err
return errors.Wrapf(err, "NetworkServiceRegistry find server failed to send a response %s", nsResp.String())
}
}
return next.NetworkServiceRegistryServer(server.Context()).Find(query, server)
Expand Down Expand Up @@ -157,7 +157,7 @@ func (s *memoryNSServer) receiveEvent(
if server.Context().Err() != nil {
return errors.WithStack(io.EOF)
}
return err
return errors.Wrapf(err, "NetworkServiceRegistry find server failed to send a response %s", nse.String())
}
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/common/memory/nse_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s *memoryNSEServer) Find(query *registry.NetworkServiceEndpointQuery, serv
NetworkServiceEndpoint: nse,
}
if err := server.Send(nseResp); err != nil {
return err
return errors.Wrapf(err, "NetworkServiceRegistry find server failed to send a response %s", nseResp.String())
}
}
return next.NetworkServiceEndpointRegistryServer(server.Context()).Find(query, server)
Expand Down Expand Up @@ -156,7 +156,7 @@ func (s *memoryNSEServer) receiveEvent(
if server.Context().Err() != nil {
return errors.WithStack(io.EOF)
}
return err
return errors.Wrapf(err, "NetworkServiceRegistry find server failed to send a response %s", event.String())
}
}
return nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/registry/common/recvfd/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
// Copyright (c) 2020-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -139,7 +139,7 @@ func (r *recvfdNseServer) Unregister(ctx context.Context, endpoint *registry.Net
func recvFDAndSwapInodeToUnix(ctx context.Context, fileMap *perEndpointFileMap, endpoint *registry.NetworkServiceEndpoint, recv grpcfd.FDRecver) error {
inodeURL, err := url.Parse(endpoint.GetUrl())
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to parse url %s", endpoint.GetUrl())
}

// Is it an inode?
Expand All @@ -154,13 +154,13 @@ func recvFDAndSwapInodeToUnix(ctx context.Context, fileMap *perEndpointFileMap,
var fileCh <-chan *os.File
fileCh, err = recv.RecvFileByURL(endpoint.GetUrl())
if err != nil {
err = errors.WithStack(err)
err = errors.Wrapf(err, "failed to receive file by url %s", endpoint.GetUrl())
return
}
// Wait for the file to arrive on the fileCh or the context to expire
select {
case <-ctx.Done():
err = ctx.Err()
err = errors.WithStack(ctx.Err())
return
case file = <-fileCh:
// If we get the file, remember it in the fileMap so we can reuse it later
Expand All @@ -185,7 +185,7 @@ func swapFileToInode(fileMap *perEndpointFileMap, endpoint *registry.NetworkServ
// Transform string to URL for correctness checking and ease of use
unixURL, err := url.Parse(endpoint.GetUrl())
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to parse url %s", endpoint.GetUrl())
}

// Is it a file?
Expand Down
10 changes: 5 additions & 5 deletions pkg/registry/common/retry/nse_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (r *retryNSEClient) Register(ctx context.Context, nse *registry.NetworkServ

select {
case <-r.chainCtx.Done():
return nil, errors.WithStack(err)
return nil, err
case <-ctx.Done():
return nil, errors.WithStack(ctx.Err())
case <-c.After(r.interval):
Expand Down Expand Up @@ -103,10 +103,10 @@ func (r *retryNSEClient) Find(ctx context.Context, query *registry.NetworkServic
}

if r.chainCtx.Err() != nil {
return nil, r.chainCtx.Err()
return nil, errors.WithStack(r.chainCtx.Err())
}

return nil, ctx.Err()
return nil, errors.WithStack(ctx.Err())
}

func (r *retryNSEClient) Unregister(ctx context.Context, in *registry.NetworkServiceEndpoint, opts ...grpc.CallOption) (*emptypb.Empty, error) {
Expand All @@ -125,7 +125,7 @@ func (r *retryNSEClient) Unregister(ctx context.Context, in *registry.NetworkSer
case <-r.chainCtx.Done():
return nil, err
case <-ctx.Done():
return nil, ctx.Err()
return nil, errors.WithStack(ctx.Err())
case <-c.After(r.interval):
continue
}
Expand All @@ -134,5 +134,5 @@ func (r *retryNSEClient) Unregister(ctx context.Context, in *registry.NetworkSer
return resp, err
}

return nil, ctx.Err()
return nil, errors.WithStack(ctx.Err())
}
10 changes: 5 additions & 5 deletions pkg/registry/common/sendfd/client_linux.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
//
// Copyright (c) 2021-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2020-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -99,7 +99,7 @@ func sendFDAndSwapFileToInode(sender grpcfd.FDSender, endpoint *registry.Network
// Transform string to URL for correctness checking and ease of use
unixURL, err := url.Parse(endpoint.Url)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to parse url %s", endpoint.Url)
}

// Is it a file?
Expand All @@ -109,14 +109,14 @@ func sendFDAndSwapFileToInode(sender grpcfd.FDSender, endpoint *registry.Network
// Translate the file to an inodeURL of the form inode://${dev}/${ino}
inodeURL, err := grpcfd.FilenameToURL(unixURL.Path)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to convert filename %s to url", unixURL.Path)
}
// Send the file
errCh := sender.SendFilename(unixURL.Path)
select {
case err := <-errCh:
// If we immediately return an error... the file probably doesn't exist
return errors.WithStack(err)
return errors.Wrapf(err, "failed to send a file %s, the file probably doesn't exist", unixURL.Path)
default:
// But don't wait for any subsequent errors... they won't arrive till after we've sent the GRPC message
}
Expand Down
Loading

0 comments on commit a00e6a2

Please sign in to comment.