From 892683bef86cacc2ccda2b4df2b079171bd92164 Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Wed, 22 Aug 2018 06:07:04 +0200 Subject: Show VPPApiError value always and remove RegisterBinAPITypes for mock adapter Change-Id: I3b216748df1a372f25cc94e3df5d7b1b2b7a8a40 Signed-off-by: Ondrej Fabry --- api/vppapi_errors.go | 14 ++++++++++++-- api/vppapi_errors_test.go | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 api/vppapi_errors_test.go (limited to 'api') diff --git a/api/vppapi_errors.go b/api/vppapi_errors.go index c921e14..18ab87f 100644 --- a/api/vppapi_errors.go +++ b/api/vppapi_errors.go @@ -5,16 +5,26 @@ import ( "strconv" ) +// RetvalToVPPApiError returns error for retval value. +// Retval 0 returns nil error. +func RetvalToVPPApiError(retval int32) error { + if retval == 0 { + return nil + } + return VPPApiError(retval) +} + // VPPApiError represents VPP's vnet API error that is usually // returned as Retval field in replies from VPP binary API. type VPPApiError int32 func (e VPPApiError) Error() string { + errid := int64(e) var errstr string if s, ok := vppApiErrors[e]; ok { - errstr = s + errstr = fmt.Sprintf("%s (%d)", s, errid) } else { - errstr = strconv.Itoa(int(e)) + errstr = strconv.FormatInt(errid, 10) } return fmt.Sprintf("VPPApiError: %s", errstr) } diff --git a/api/vppapi_errors_test.go b/api/vppapi_errors_test.go new file mode 100644 index 0000000..78e1fbf --- /dev/null +++ b/api/vppapi_errors_test.go @@ -0,0 +1,23 @@ +package api + +import ( + "testing" + + . "github.com/onsi/gomega" +) + +func TestUnspecified(t *testing.T) { + RegisterTestingT(t) + + var err error = VPPApiError(-1) + errstr := err.Error() + Expect(errstr).Should(BeEquivalentTo("VPPApiError: Unspecified Error (-1)")) +} + +func TestUnknown(t *testing.T) { + RegisterTestingT(t) + + var err error = VPPApiError(-999) + errstr := err.Error() + Expect(errstr).Should(BeEquivalentTo("VPPApiError: -999")) +} -- cgit 1.2.3-korg