From 9f4240a4f17df64e58fd183df968e9bee63a7b30 Mon Sep 17 00:00:00 2001 From: Malte Bauch <malte.bauch@stud.h-da.de> Date: Tue, 22 Feb 2022 19:29:32 +0000 Subject: [PATCH] Resolve "Custom error values are not provided" --- nucleus/errors/errors.go | 2 +- nucleus/gnmi_transport.go | 27 +++++++++++++++------ nucleus/principalNetworkDomain.go | 10 ++++++-- nucleus/southbound.go | 10 ++++++-- store/changeStores.go | 14 +++++------ store/deviceStore.go | 4 +-- store/pndStore.go | 2 +- store/sbiStore.go | 2 +- test/integration/nucleusIntegration_test.go | 10 ++++++-- 9 files changed, 55 insertions(+), 26 deletions(-) diff --git a/nucleus/errors/errors.go b/nucleus/errors/errors.go index d85599bf8..43bce0f1d 100644 --- a/nucleus/errors/errors.go +++ b/nucleus/errors/errors.go @@ -58,7 +58,7 @@ type ErrInvalidTypeAssertion struct { } func (e ErrInvalidTypeAssertion) Error() string { - return fmt.Sprintf("%v does not implement %v", e.Value, e.Type) + return fmt.Sprintf("%v does not implement %v", reflect.TypeOf(e.Value).Elem(), reflect.TypeOf(e.Type).Elem()) } // ErrUnsupportedPath implements the Error interface and is called if the diff --git a/nucleus/gnmi_transport.go b/nucleus/gnmi_transport.go index 8b56dc80e..37f6f7726 100644 --- a/nucleus/gnmi_transport.go +++ b/nucleus/gnmi_transport.go @@ -3,7 +3,6 @@ package nucleus import ( "context" "fmt" - "reflect" "code.fbi.h-da.de/danet/gosdn/interfaces/change" @@ -39,9 +38,15 @@ type Gnmi struct { // Do not call directly. Use NewTransport() instead. func newGnmiTransport(opts *tpb.TransportOption, sbi southbound.SouthboundInterface) (*Gnmi, error) { if opts == nil || sbi == nil { - return nil, &errors.ErrInvalidParameters{} + return nil, &errors.ErrInvalidParameters{ + Func: newGnmiTransport, + Param: "'opts' and 'sbi' can not be nil", + } } else if opts.TransportOption == nil { - return nil, &errors.ErrInvalidParameters{} + return nil, &errors.ErrInvalidParameters{ + Func: newGnmiTransport, + Param: "'opts.TransportOption' can not be nil", + } } gnmiConfig := &gnmi.Config{ Addr: opts.Address, @@ -102,7 +107,7 @@ func (g *Gnmi) applyDiff(ctx context.Context, payload change.Payload) error { case ppb.ApiOperation_REPLACE: req.Replace = diff.Update default: - return &errors.ErrOperationNotSupported{} + return &errors.ErrOperationNotSupported{Op: op} } } else if diff.Delete != nil { req.Delete = diff.Delete @@ -131,11 +136,17 @@ func (g *Gnmi) Type() string { func (g *Gnmi) ProcessResponse(resp interface{}, root interface{}, s *ytypes.Schema) error { d, ok := root.(ygot.ValidatedGoStruct) if !ok { - return &errors.ErrInvalidTypeAssertion{} + return &errors.ErrInvalidTypeAssertion{ + Value: root, + Type: (*ygot.ValidatedGoStruct)(nil), + } } r, ok := resp.(*gpb.GetResponse) if !ok { - return &errors.ErrInvalidTypeAssertion{} + return &errors.ErrInvalidTypeAssertion{ + Value: resp, + Type: &gpb.GetResponse{}, + } } rn := r.Notification errs := make([]error, 0) @@ -219,8 +230,8 @@ func (g *Gnmi) subscribe(ctx context.Context) error { opts, ok := ctx.Value(types.CtxKeyOpts).(*gnmi.SubscribeOptions) if !ok { return &errors.ErrInvalidTypeAssertion{ - Value: reflect.TypeOf(ctx.Value(types.CtxKeyOpts)), - Type: reflect.TypeOf(&gnmi.SubscribeOptions{}), + Value: ctx.Value(types.CtxKeyOpts), + Type: &gnmi.SubscribeOptions{}, } } go func() { diff --git a/nucleus/principalNetworkDomain.go b/nucleus/principalNetworkDomain.go index b5dd33d7a..94a314362 100644 --- a/nucleus/principalNetworkDomain.go +++ b/nucleus/principalNetworkDomain.go @@ -290,7 +290,10 @@ func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) (proto.Messag } resp, ok := res.(proto.Message) if !ok { - return nil, &errors.ErrInvalidTypeAssertion{} + return nil, &errors.ErrInvalidTypeAssertion{ + Value: res, + Type: (*proto.Message)(nil), + } } err = d.ProcessResponse(resp) if err != nil { @@ -515,7 +518,10 @@ func loadPlugin(id uuid.UUID) (southbound.SouthboundInterface, error) { var sbi southbound.SouthboundInterface sbi, ok := symbol.(southbound.SouthboundInterface) if !ok { - return nil, &errors.ErrInvalidTypeAssertion{} + return nil, &errors.ErrInvalidTypeAssertion{ + Value: symbol, + Type: (*southbound.SouthboundInterface)(nil), + } } log.WithFields(log.Fields{ "identifier": sbi.SbiIdentifier(), diff --git a/nucleus/southbound.go b/nucleus/southbound.go index 7271eb8d4..d62411738 100644 --- a/nucleus/southbound.go +++ b/nucleus/southbound.go @@ -91,7 +91,10 @@ func unmarshal(schema *ytypes.Schema, bytes []byte, path *gpb.Path, goStruct ygo } validatedDeepCopy, ok := root.(ygot.ValidatedGoStruct) if !ok { - return &errors.ErrInvalidTypeAssertion{} + return &errors.ErrInvalidTypeAssertion{ + Value: root, + Type: (*ygot.ValidatedGoStruct)(nil), + } } // returns the node we want to fill with the data contained in 'bytes', @@ -102,7 +105,10 @@ func unmarshal(schema *ytypes.Schema, bytes []byte, path *gpb.Path, goStruct ygo } validatedCreatedNode, ok := createdNode.(ygot.ValidatedGoStruct) if !ok { - return &errors.ErrInvalidTypeAssertion{} + return &errors.ErrInvalidTypeAssertion{ + Value: createdNode, + Type: (*ygot.ValidatedGoStruct)(nil), + } } if err := openconfig.Unmarshal(bytes, validatedCreatedNode, opt...); err != nil { diff --git a/store/changeStores.go b/store/changeStores.go index 974c6f13e..ef2d5a750 100644 --- a/store/changeStores.go +++ b/store/changeStores.go @@ -26,17 +26,17 @@ func (s *ChangeStore) GetChange(id uuid.UUID) (change.Change, error) { if err != nil { return nil, err } - change, ok := item.(change.Change) + c, ok := item.(change.Change) if !ok { return nil, &errors.ErrInvalidTypeAssertion{ - Value: change, - Type: "change", + Value: c, + Type: (*change.Change)(nil), } } log.WithFields(log.Fields{ "uuid": id, }).Debug("change was accessed") - return change, nil + return c, nil } // Pending returns the UUIDs of all pending changes @@ -57,10 +57,10 @@ func (s *ChangeStore) Confirmed() []uuid.UUID { func filterChanges(store *ChangeStore, state ppb.Change_State) []uuid.UUID { changes := make([]uuid.UUID, 0) for _, ch := range store.Store { - switch change := ch.(type) { + switch c := ch.(type) { case change.Change: - if change.State() == state { - changes = append(changes, change.ID()) + if c.State() == state { + changes = append(changes, c.ID()) } } } diff --git a/store/deviceStore.go b/store/deviceStore.go index c55927c4e..871c096f8 100644 --- a/store/deviceStore.go +++ b/store/deviceStore.go @@ -44,7 +44,7 @@ func (s *DeviceStore) GetDevice(id uuid.UUID, parseErrors ...error) (device.Devi myID, ok := s.DeviceNameToUUIDLookup[e.DeviceName] if !ok { log.Debug(fmt.Sprintf("no device named %s found", foundID)) - return nil, &errors.ErrNotFound{} + return nil, &errors.ErrNotFound{ID: foundID} } foundID = myID @@ -60,7 +60,7 @@ func (s *DeviceStore) GetDevice(id uuid.UUID, parseErrors ...error) (device.Devi if !ok { return nil, &errors.ErrInvalidTypeAssertion{ Value: d, - Type: reflect.TypeOf((device.Device)(nil)), + Type: (*device.Device)(nil), } } log.WithFields(log.Fields{ diff --git a/store/pndStore.go b/store/pndStore.go index b1b8d3fc5..deff904fd 100644 --- a/store/pndStore.go +++ b/store/pndStore.go @@ -47,7 +47,7 @@ func (s *PndStore) GetPND(id uuid.UUID) (networkdomain.NetworkDomain, error) { if !ok { return nil, &errors.ErrInvalidTypeAssertion{ Value: pnd, - Type: "PrincipalNetworkDomain", + Type: (*networkdomain.NetworkDomain)(nil), } } log.WithFields(log.Fields{ diff --git a/store/sbiStore.go b/store/sbiStore.go index fd8310bfd..f74d31092 100644 --- a/store/sbiStore.go +++ b/store/sbiStore.go @@ -29,7 +29,7 @@ func (s *SbiStore) GetSBI(id uuid.UUID) (southbound.SouthboundInterface, error) if !ok { return nil, &errors.ErrInvalidTypeAssertion{ Value: sbi, - Type: "SouthboundInterface", + Type: (*southbound.SouthboundInterface)(nil), } } log.WithFields(log.Fields{ diff --git a/test/integration/nucleusIntegration_test.go b/test/integration/nucleusIntegration_test.go index cf7b86936..68ac90710 100644 --- a/test/integration/nucleusIntegration_test.go +++ b/test/integration/nucleusIntegration_test.go @@ -228,7 +228,10 @@ func TestGnmi_SetValidIntegration(t *testing.T) { } r, ok := resp.(*gpb.GetResponse) if !ok { - t.Error(&errors.ErrInvalidTypeAssertion{}) + t.Error(&errors.ErrInvalidTypeAssertion{ + Value: resp, + Type: &gpb.GetResponse{}, + }) return } got := r.Notification[0].Update[0].Val.GetStringVal() @@ -485,7 +488,10 @@ func TestGnmi_CapabilitiesIntegration(t *testing.T) { } g, ok := tr.(*nucleus.Gnmi) if !ok { - t.Error(&errors.ErrInvalidTypeAssertion{}) + t.Error(&errors.ErrInvalidTypeAssertion{ + Value: tr, + Type: &nucleus.Gnmi{}, + }) } resp, err := g.Capabilities(tt.args.ctx) if (err != nil) != tt.wantErr { -- GitLab