From aec50fa4cec2ed96ca92db3707c5343cbe5c646d Mon Sep 17 00:00:00 2001 From: Tibor Kiss <32801432+the1bit@users.noreply.github.com> Date: Thu, 1 Feb 2024 12:26:13 +0100 Subject: [PATCH] Fix lint: goerr113 (#71) * Add static errors * Linter config * Fix clean message * Fix minor issues --- .github/workflows/lint-go.yml | 2 +- pkg/application/lister.go | 4 +++- pkg/cloudevents/builder/generic.go | 4 +++- pkg/cloudevents/eventtype/parse.go | 9 +++++++-- pkg/handler/handler_v1alpha1_test.go | 6 ++++-- pkg/nats/connect.go | 4 +++- pkg/signals/signals.go | 4 +++- 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/.github/workflows/lint-go.yml b/.github/workflows/lint-go.yml index 41e84f6..4392454 100644 --- a/.github/workflows/lint-go.yml +++ b/.github/workflows/lint-go.yml @@ -16,4 +16,4 @@ jobs: uses: kyma-project/eventing-tools/.github/workflows/lint-go-reusable.yml@main with: go-version: '1.21' - lint-config-uri: https://raw.githubusercontent.com/kyma-project/eventing-tools/4bc0e923b67ddb0a028171d8beebc3cb4a211d69/config/lint/.golangci.yaml + lint-config-uri: https://raw.githubusercontent.com/kyma-project/eventing-tools/cf7f3aebaad2d323730a9ab2a76f6cb78f459ee4/config/lint/.golangci.yaml diff --git a/pkg/application/lister.go b/pkg/application/lister.go index ee1025c..bf54afe 100644 --- a/pkg/application/lister.go +++ b/pkg/application/lister.go @@ -18,6 +18,8 @@ import ( emlogger "github.com/kyma-project/eventing-manager/pkg/logger" ) +var ErrFailedToConvertObjectToUnstructured = errors.New("failed to convert runtime object to unstructured") + type Lister struct { lister cache.GenericLister } @@ -41,7 +43,7 @@ func (l Lister) Get(name string) (*kymaappconnv1alpha1.Application, error) { u, ok := object.(*unstructured.Unstructured) if !ok { - return nil, errors.New("failed to convert runtime object to unstructured") + return nil, ErrFailedToConvertObjectToUnstructured } a := &kymaappconnv1alpha1.Application{} diff --git a/pkg/cloudevents/builder/generic.go b/pkg/cloudevents/builder/generic.go index 1273652..ea7292c 100644 --- a/pkg/cloudevents/builder/generic.go +++ b/pkg/cloudevents/builder/generic.go @@ -12,6 +12,8 @@ import ( "github.com/kyma-project/eventing-manager/pkg/logger" ) +var ErrEventTypeCannotHaveEmptySegments = fmt.Errorf("event type cannot have empty segments after cleaning") + // Perform a compile-time check. var _ CloudEventBuilder = &GenericBuilder{} @@ -57,7 +59,7 @@ func (gb *GenericBuilder) Build(event ceevent.Event) (*ceevent.Event, error) { // validate if the segments are not empty segments := strings.Split(finalEventType, ".") if DoesEmptySegmentsExist(segments) { - return nil, fmt.Errorf("event type cannot have empty segments after cleaning: %s", finalEventType) + return nil, fmt.Errorf("%w: %s", ErrEventTypeCannotHaveEmptySegments, finalEventType) } namedLogger.Debugf("using event type: %s", finalEventType) diff --git a/pkg/cloudevents/eventtype/parse.go b/pkg/cloudevents/eventtype/parse.go index f157efd..eb06e8c 100644 --- a/pkg/cloudevents/eventtype/parse.go +++ b/pkg/cloudevents/eventtype/parse.go @@ -8,6 +8,11 @@ import ( "github.com/kyma-project/eventing-publisher-proxy/pkg/cloudevents/builder" ) +var ( // Static errors. + ErrPrefixNotFound = errors.New("prefix not found") + ErrInvalidFormat = errors.New("invalid format") +) + // parse splits the event-type using the given prefix and returns the application name, event and version // or an error if the event-type format is invalid. // A valid even-type format should be: prefix.application.event.version or application.event.version @@ -15,7 +20,7 @@ import ( // Constraint: the application segment in the input event-type should not contain ".". func parse(eventType, prefix string) (string, string, string, error) { if !strings.HasPrefix(eventType, prefix) { - return "", "", "", errors.New("prefix not found") + return "", "", "", ErrPrefixNotFound } // remove the prefix @@ -26,7 +31,7 @@ func parse(eventType, prefix string) (string, string, string, error) { // (e.g. application.businessObject.operation.version) parts := strings.Split(eventType, ".") if len(parts) < 4 || builder.DoesEmptySegmentsExist(parts) { - return "", "", "", errors.New("invalid format") + return "", "", "", ErrInvalidFormat } // parse the event-type segments diff --git a/pkg/handler/handler_v1alpha1_test.go b/pkg/handler/handler_v1alpha1_test.go index df75f30..a6a356e 100644 --- a/pkg/handler/handler_v1alpha1_test.go +++ b/pkg/handler/handler_v1alpha1_test.go @@ -34,6 +34,8 @@ import ( emlogger "github.com/kyma-project/eventing-manager/pkg/logger" ) +var ErrUnableToClean = fmt.Errorf("unable to clean") + func Test_extractCloudEventFromRequest(t *testing.T) { type args struct { request *http.Request @@ -253,14 +255,14 @@ func TestHandler_publishCloudEvents_v1alpha1(t *testing.T) { collector: metrics.NewCollector(latency), eventTypeCleaner: &eventtypetest.CleanerStub{ CleanType: "", - Error: fmt.Errorf("I cannot clean"), + Error: ErrUnableToClean, }, }, args: args{ request: CreateValidBinaryRequestV1Alpha1(t), }, wantStatus: 400, - wantBody: []byte("I cannot clean"), + wantBody: []byte("unable to clean"), wantTEF: "", // client error will not be recorded as EPP internal error. So no metric will be updated. }, { diff --git a/pkg/nats/connect.go b/pkg/nats/connect.go index ba17023..0d9a3b7 100644 --- a/pkg/nats/connect.go +++ b/pkg/nats/connect.go @@ -6,6 +6,8 @@ import ( "github.com/nats-io/nats.go" ) +var ErrNATSConnectionNotConnected = fmt.Errorf("NATS connection not connected") + type Opt = nats.Option //nolint:gochecknoglobals // cloning functions as variables. @@ -27,7 +29,7 @@ func Connect(url string, opts ...Opt) (*nats.Conn, error) { } if status := connection.Status(); status != nats.CONNECTED { - return nil, fmt.Errorf("NATS connection not connected with status:%v", status) + return nil, fmt.Errorf("%w with status:%v", ErrNATSConnectionNotConnected, status) } return connection, err diff --git a/pkg/signals/signals.go b/pkg/signals/signals.go index dcf01e0..5d742fc 100644 --- a/pkg/signals/signals.go +++ b/pkg/signals/signals.go @@ -9,6 +9,8 @@ import ( "time" ) +var ErrReceivedTerminationSignal = errors.New("received a termination signal") + //nolint:gochecknoglobals // defining channels var ( // onlyOneSignalHandler to make sure that only one signal handler is registered. @@ -70,7 +72,7 @@ func (scc *signalContext) Err() error { select { case _, ok := <-scc.Done(): if !ok { - return errors.New("received a termination signal") + return ErrReceivedTerminationSignal } default: }