Skip to content

Commit

Permalink
update errors handling in pkg/networkservice
Browse files Browse the repository at this point in the history
Signed-off-by: Ruslan Bayandinov <wazsone@ya.ru>
  • Loading branch information
wazsone committed Jan 31, 2023
1 parent 7c60590 commit 2d18192
Show file tree
Hide file tree
Showing 27 changed files with 91 additions and 66 deletions.
9 changes: 6 additions & 3 deletions pkg/networkservice/chains/endpoint/combine_monitor_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func (m *combineMonitorServer) MonitorConnections(selector *networkservice.Monit

var err error
if rv := monitorErr.Load(); rv != nil {
err = rv.(error)
err = errors.Wrap(rv.(error), "an error occurred during monitor connections")
}
return errors.WithStack(err)
return err
}

func startMonitorConnectionsServer(
Expand Down Expand Up @@ -135,7 +135,10 @@ func (m *combineMonitorConnectionsServer) Send(event *networkservice.ConnectionE
return errors.WithStack(err)
default:
m.initWg.Wait()
return errors.WithStack(m.MonitorConnection_MonitorConnectionsServer.Send(event))
if err := m.MonitorConnection_MonitorConnectionsServer.Send(event); err != nil {
return errors.Wrapf(err, "MonitorConnections server failed to send an event %s", event.String())
}
return nil
}
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/networkservice/common/authorize/common.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2021 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 All @@ -20,6 +22,7 @@ import (
"context"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/pkg/errors"
)

// Policy represents authorization policy for network service.
Expand All @@ -39,7 +42,7 @@ func (l *policiesList) check(ctx context.Context, p *networkservice.Path) error
continue
}
if err := policy.Check(ctx, p); err != nil {
return err
return errors.Wrap(err, "an error occurred during authorization policy check")
}
}
return nil
Expand Down
5 changes: 3 additions & 2 deletions pkg/networkservice/common/authorize/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -34,6 +34,7 @@ import (
"time"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -168,7 +169,7 @@ func TestAuthzEndpoint(t *testing.T) {
return
}
require.NotNil(t, err, "request expected to be denied")
s, ok := status.FromError(err)
s, ok := status.FromError(errors.Cause(err))
require.True(t, ok, "error without error status code"+err.Error())
require.Equal(t, s.Code(), codes.PermissionDenied, "wrong error status code")
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/networkservice/common/discover/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (d *discoverCandidatesServer) Request(ctx context.Context, request *network
}
u, err := url.Parse(nse.Url)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrapf(err, "failed to parse url %s", nse.Url)
}
return next.Server(ctx).Request(clienturlctx.WithClientURL(ctx, u), request)
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func (d *discoverCandidatesServer) Close(ctx context.Context, conn *networkservi
if nse, err := d.discoverNetworkServiceEndpoint(ctx, nseName); err == nil {
u, err = url.Parse(nse.Url)
if err != nil {
return nil, errors.WithStack(err)
return nil, errors.Wrapf(err, "failed to parse url %s", nse.Url)
}
} else {
logger.Errorf("no endpoint found for Close: %v", conn)
Expand All @@ -116,7 +116,7 @@ func (d *discoverCandidatesServer) discoverNetworkServiceEndpoint(ctx context.Co

nseRespStream, err := d.nseClient.Find(ctx, query)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to find %s", query.String())
}
nseList := registry.ReadNetworkServiceEndpointList(nseRespStream)

Expand All @@ -140,7 +140,7 @@ func (d *discoverCandidatesServer) discoverNetworkServiceEndpoints(ctx context.C

nseRespStream, err := d.nseClient.Find(ctx, query)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to find %s", query.String())
}
nseList := registry.ReadNetworkServiceEndpointList(nseRespStream)

Expand All @@ -162,7 +162,7 @@ func (d *discoverCandidatesServer) discoverNetworkService(ctx context.Context, n

nsRespStream, err := d.nsClient.Find(ctx, query)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to find %s", query.String())
}
nsList := registry.ReadNetworkServiceList(nsRespStream)

Expand Down
12 changes: 6 additions & 6 deletions pkg/networkservice/common/discoverforwarder/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (d *discoverForwarderServer) Request(ctx context.Context, request *networks
})
if err != nil {
logger.Errorf("can not open registry nse stream by networkservice. Error: %v", err.Error())
return nil, err
return nil, errors.Wrapf(err, "failed to find %s on %s", d.forwarderServiceName, d.nsmgrURL)
}

nses := d.matchForwarders(request.Connection.GetLabels(), ns, registry.ReadNetworkServiceEndpointList(stream))
Expand Down Expand Up @@ -135,7 +135,7 @@ func (d *discoverForwarderServer) Request(ctx context.Context, request *networks
})
if err != nil {
logger.Errorf("can not open registry nse stream by forwarder name. Error: %v", err.Error())
return nil, err
return nil, errors.Wrapf(err, "failed to find %s on %s", forwarderName, d.nsmgrURL)
}

nses := registry.ReadNetworkServiceEndpointList(stream)
Expand All @@ -147,7 +147,7 @@ func (d *discoverForwarderServer) Request(ctx context.Context, request *networks
u, err := url.Parse(nses[0].Url)
if err != nil {
logger.Errorf("can not parse forwarder=%v url=%v error=%v", nses[0].Name, u, err.Error())
return nil, errors.WithStack(err)
return nil, errors.Wrapf(err, "failed to parse url %s", nses[0].Url)
}

conn, err := next.Server(ctx).Request(clienturlctx.WithClientURL(ctx, u), request)
Expand All @@ -172,7 +172,7 @@ func (d *discoverForwarderServer) Close(ctx context.Context, conn *networkservic
})
if err != nil {
logger.Errorf("can not open registry nse stream by forwarder name. Error: %v", err.Error())
return nil, err
return nil, errors.Wrapf(err, "failed to find %s on %s", forwarderName, d.nsmgrURL)
}

nses := registry.ReadNetworkServiceEndpointList(stream)
Expand All @@ -183,7 +183,7 @@ func (d *discoverForwarderServer) Close(ctx context.Context, conn *networkservic
u, err := url.Parse(nses[0].Url)
if err != nil {
logger.Errorf("can not parse forwarder url %v", err.Error())
return nil, errors.WithStack(err)
return nil, errors.Wrapf(err, "failed to parse url %s", nses[0].Url)
}

ctx = clienturlctx.WithClientURL(ctx, u)
Expand Down Expand Up @@ -238,7 +238,7 @@ func (d *discoverForwarderServer) discoverNetworkService(ctx context.Context, na

nsRespStream, err := d.nsClient.Find(ctx, query)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to find %s", query.String())
}

nsList := registry.ReadNetworkServiceList(nsRespStream)
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/common/excludedprefixes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func validateIPs(ipContext *networkservice.IPContext, excludedPrefixes []string)
for _, prefix := range excludedPrefixes {
_, ipNet, err := net.ParseCIDR(prefix)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to parse %s as CIDR", prefix)
}
// TODO: Think about validating routes with prefixes size less than /32 and /128
if prefixLen, maxLen := ipNet.Mask.Size(); prefixLen != maxLen {
Expand All @@ -227,7 +227,7 @@ func validateIPs(ipContext *networkservice.IPContext, excludedPrefixes []string)
for _, prefix := range prefixes {
ip, _, err := net.ParseCIDR(prefix)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to parse %s as CIDR", prefix)
}

if ip4Pool.Contains(ip) || ip6Pool.Contains(ip) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/networkservice/common/heal/eventloop.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 @@ -66,14 +66,14 @@ func newEventLoop(ctx context.Context, cc grpc.ClientConnInterface, conn *networ
client, err := networkservice.NewMonitorConnectionClient(cc).MonitorConnections(eventLoopCtx, selector)
if err != nil {
eventLoopCancel()
return nil, errors.WithStack(err)
return nil, errors.Wrap(err, "failed get MonitorConnections client")
}

// get the initial state transfer and use it to detect whether we have a real connection or not
_, err = client.Recv()
if err != nil {
eventLoopCancel()
return nil, errors.WithStack(err)
return nil, errors.Wrap(err, "failed to get the initial state transfer")
}

logger := log.FromContext(ctx).WithField("heal", "eventLoop")
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/common/journal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ func (srv *journalServer) Close(ctx context.Context, connection *networkservice.
func (srv *journalServer) publish(entry *Entry) error {
js, err := json.Marshal(entry)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to get JSON of %s", entry)
}

err = srv.nats.Publish(srv.journalID, js)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to publish %s to the cluster %s", js, srv.journalID)
}
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/networkservice/common/mechanisms/recvfd/common.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 @@ -39,7 +39,7 @@ func recvFDAndSwapInodeToFile(ctx context.Context, fileMap *perConnectionFileMap
// Transform string to URL for correctness checking and ease of use
inodeURL, err := url.Parse(inodeURLStr)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to parse url %s", inodeURLStr)
}

// Is it an inode?
Expand All @@ -55,7 +55,7 @@ func recvFDAndSwapInodeToFile(ctx context.Context, fileMap *perConnectionFileMap
var fileCh <-chan *os.File
fileCh, err = recv.RecvFileByURL(inodeURLStr)
if err != nil {
err = errors.WithStack(err)
err = errors.Wrapf(err, "failed to receive file by url %s", inodeURLStr)
return
}
// Wait for the file to arrive on the fileCh or the context to expire
Expand Down Expand Up @@ -97,7 +97,7 @@ func swapFileToInode(fileMap *perConnectionFileMap, parameters map[string]string
// Transform string to URL for correctness checking and ease of use
fileURL, err := url.Parse(fileURLStr)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to parse url %s", fileURLStr)
}

// Is it a file?
Expand Down
8 changes: 4 additions & 4 deletions pkg/networkservice/common/mechanisms/sendfd/common.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 @@ -41,7 +41,7 @@ func sendFDAndSwapFileToInode(sender grpcfd.FDSender, parameters, inodeURLToFile
// Transform string to URL for correctness checking and ease of use
fileURL, err := url.Parse(fileURLStr)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to parse url %s", fileURLStr)
}

// Is it a file?
Expand All @@ -51,14 +51,14 @@ func sendFDAndSwapFileToInode(sender grpcfd.FDSender, parameters, inodeURLToFile
// Translate the file to an inodeURL of the form inode://${dev}/${ino}
inodeURL, err := grpcfd.FilenameToURL(fileURL.Path)
if err != nil {
return errors.WithStack(err)
return errors.Wrapf(err, "failed to convert a filename %s to URL", fileURL.Path)
}
// Send the file
errCh := sender.SendFilename(fileURL.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)", fileURL.Path)
default:
// But don't wait for any subsequent errors... they won't arrive till after we've sent the GRPC message
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/networkservice/common/mechanisms/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// 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 @@ -59,7 +59,7 @@ func (ms *mechanismsServer) Request(ctx context.Context, request *networkservice
if ok {
return srv.Request(ctx, request)
}
return nil, errUnsupportedMech
return nil, errors.WithStack(errUnsupportedMech)
}
var err = errCannotSupportMech
for _, mechanism := range request.GetMechanismPreferences() {
Expand Down
5 changes: 4 additions & 1 deletion pkg/networkservice/common/mechanisms/vxlan/vni/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// Copyright (c) 2021 Nordix Foundation.
//
// Copyright (c) 2023 Cisco Systems, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -26,6 +28,7 @@ import (
"github.com/networkservicemesh/sdk/pkg/tools/log"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/vxlan"
Expand Down Expand Up @@ -102,7 +105,7 @@ func (v *vniServer) Request(ctx context.Context, request *networkservice.Network
var err error
k.vni, err = mechanism.GenerateRandomVNI()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to generate a random VNI")
}
// If its not one already in use, set it and we are good to go
if _, ok := v.Map.LoadOrStore(k, &k); !ok {
Expand Down
5 changes: 4 additions & 1 deletion pkg/networkservice/common/monitor/client_filter.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 Cisco 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 All @@ -18,6 +20,7 @@ package monitor

import (
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/pkg/errors"
)

type clientFilter struct {
Expand All @@ -36,7 +39,7 @@ func (c *clientFilter) Recv() (*networkservice.ConnectionEvent, error) {
for {
eventIn, err := c.MonitorConnection_MonitorConnectionsClient.Recv()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "MonitorConnections client failed to receive an event")
}
eventOut := &networkservice.ConnectionEvent{
Type: networkservice.ConnectionEventType_UPDATE,
Expand Down
6 changes: 3 additions & 3 deletions pkg/networkservice/common/monitor/eventloop.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 @@ -54,14 +54,14 @@ func newEventLoop(ctx context.Context, ec EventConsumer, cc grpc.ClientConnInter
client, err := networkservice.NewMonitorConnectionClient(cc).MonitorConnections(eventLoopCtx, selector)
if err != nil {
eventLoopCancel()
return nil, errors.WithStack(err)
return nil, errors.Wrap(err, "failed to get a MonitorConnections client")
}

// get the initial state transfer and use it to detect whether we have a real connection or not
_, err = client.Recv()
if err != nil {
eventLoopCancel()
return nil, errors.WithStack(err)
return nil, errors.Wrap(err, "failed to get the initial state transfer")
}

cev := &eventLoop{
Expand Down
Loading

0 comments on commit 2d18192

Please sign in to comment.