From 5eca374b1e8f4e67a094c65d293e73d8af740aa9 Mon Sep 17 00:00:00 2001 From: Spencer Schrock Date: Mon, 23 Oct 2023 16:10:04 -0700 Subject: [PATCH] :seedling: enable style linter `errname` (#3587) * enable errname linter Signed-off-by: Spencer Schrock * convert publish err to custom error type. Signed-off-by: Spencer Schrock * remove unused exported error. Signed-off-by: Spencer Schrock * convert unsupported exporter type to custom error type. Signed-off-by: Spencer Schrock * exempt public errors from linter. Signed-off-by: Spencer Schrock * exempt cron config errors from linter. Signed-off-by: Spencer Schrock --------- Signed-off-by: Spencer Schrock --- .golangci.yml | 1 + cron/config/config.go | 9 ++++++--- cron/internal/pubsub/publisher.go | 11 ++++++++--- cron/internal/pubsub/subscriber.go | 6 +----- cron/monitoring/exporter.go | 11 ++++++++--- errors/public.go | 13 ++++++++----- 6 files changed, 32 insertions(+), 19 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index e1b21d9a..9d3048d8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -26,6 +26,7 @@ linters: - asciicheck - dogsled - errcheck + - errname - errorlint - exhaustive - exportloopref diff --git a/cron/config/config.go b/cron/config/config.go index a8bfb2f7..c359254a 100644 --- a/cron/config/config.go +++ b/cron/config/config.go @@ -59,12 +59,15 @@ const ( ) var ( + // some of these errors didn't follow naming conventions when they were introduced. + // for backward compatibility reasons, they can't be changed and have nolint directives. + // ErrorEmptyConfigValue indicates the value for the configuration option was empty. - ErrorEmptyConfigValue = errors.New("config value set to empty") + ErrorEmptyConfigValue = errors.New("config value set to empty") //nolint:errname // ErrorValueConversion indicates an unexpected type was found for the value of the config option. - ErrorValueConversion = errors.New("unexpected type, cannot convert value") + ErrorValueConversion = errors.New("unexpected type, cannot convert value") //nolint:errname // ErrorNoConfig indicates no config file was provided, or flag.Parse() was not called. - ErrorNoConfig = errors.New("no configuration file provided with --" + configFlag) + ErrorNoConfig = errors.New("no configuration file provided with --" + configFlag) //nolint:errname //go:embed config.yaml configYAML []byte configFilename = flag.String(configFlag, configDefault, configUsage) diff --git a/cron/internal/pubsub/publisher.go b/cron/internal/pubsub/publisher.go index 5adf752f..1c2d949f 100644 --- a/cron/internal/pubsub/publisher.go +++ b/cron/internal/pubsub/publisher.go @@ -17,7 +17,6 @@ package pubsub import ( "context" - "errors" "fmt" "log" "sync" @@ -31,7 +30,13 @@ import ( "github.com/ossf/scorecard/v4/cron/data" ) -var errorPublish = errors.New("total errors when publishing") +type publishError struct { + count uint64 +} + +func (pe publishError) Error() string { + return fmt.Sprintf("total errors when publishing: %d", pe.count) +} // Publisher interface is used to publish cron job requests to PubSub. type Publisher interface { @@ -88,7 +93,7 @@ func (publisher *publisherImpl) Publish(request *data.ScorecardBatchRequest) err func (publisher *publisherImpl) Close() error { publisher.wg.Wait() if publisher.totalErrors > 0 { - return fmt.Errorf("%w: %d", errorPublish, publisher.totalErrors) + return publishError{count: publisher.totalErrors} } return nil } diff --git a/cron/internal/pubsub/subscriber.go b/cron/internal/pubsub/subscriber.go index 693721e4..1e40acba 100644 --- a/cron/internal/pubsub/subscriber.go +++ b/cron/internal/pubsub/subscriber.go @@ -16,7 +16,6 @@ package pubsub import ( "context" - "errors" "fmt" "os" @@ -25,9 +24,6 @@ import ( "github.com/ossf/scorecard/v4/cron/data" ) -// ErrorInParse indicates there was an error while unmarshalling the protocol buffer message. -var ErrorInParse = errors.New("error during protojson.Unmarshal") - // Subscriber interface is used pull messages from PubSub. type Subscriber interface { SynchronousPull() (*data.ScorecardBatchRequest, error) @@ -49,7 +45,7 @@ func CreateSubscriber(ctx context.Context, subscriptionURL string) (Subscriber, func parseJSONToRequest(jsonData []byte) (*data.ScorecardBatchRequest, error) { ret := &data.ScorecardBatchRequest{} if err := protojson.Unmarshal(jsonData, ret); err != nil { - return nil, fmt.Errorf("%w: %v", ErrorInParse, err) + return nil, fmt.Errorf("protojson.Unmarshal: %w", err) } return ret, nil } diff --git a/cron/monitoring/exporter.go b/cron/monitoring/exporter.go index 91de0391..f0c2ef21 100644 --- a/cron/monitoring/exporter.go +++ b/cron/monitoring/exporter.go @@ -16,7 +16,6 @@ package monitoring import ( - "errors" "fmt" "time" @@ -27,7 +26,13 @@ import ( "github.com/ossf/scorecard/v4/cron/config" ) -var errorUndefinedExporter = errors.New("unsupported exporterType") +type unsupportedExporterError struct { + exporter string +} + +func (u unsupportedExporterError) Error() string { + return "unsupported exporterType: " + u.exporter +} type exporterType string @@ -59,7 +64,7 @@ func GetExporter() (Exporter, error) { case printer: return new(printerExporter), nil default: - return nil, fmt.Errorf("%w: %s", errorUndefinedExporter, exporter) + return nil, unsupportedExporterError{exporter: exporter} } } diff --git a/errors/public.go b/errors/public.go index 0e92e139..3d6b380d 100644 --- a/errors/public.go +++ b/errors/public.go @@ -20,20 +20,23 @@ import ( ) var ( + // some of these errors didn't follow naming conventions when they were introduced. + // for backward compatibility reasons, they can't be changed and have nolint directives. + // ErrScorecardInternal indicates a runtime error in Scorecard code. ErrScorecardInternal = errors.New("internal error") // ErrRepoUnreachable indicates Scorecard is unable to establish connection with the repository. ErrRepoUnreachable = errors.New("repo unreachable") // ErrorUnsupportedHost indicates the repo's host is unsupported. - ErrorUnsupportedHost = errors.New("unsupported host") + ErrorUnsupportedHost = errors.New("unsupported host") //nolint:errname // ErrorInvalidURL indicates the repo's full URL was not passed. - ErrorInvalidURL = errors.New("invalid repo flag") + ErrorInvalidURL = errors.New("invalid repo flag") //nolint:errname // ErrorShellParsing indicates there was an error when parsing shell code. - ErrorShellParsing = errors.New("error parsing shell code") + ErrorShellParsing = errors.New("error parsing shell code") //nolint:errname // ErrorUnsupportedCheck indicates check cannot be run for given request. - ErrorUnsupportedCheck = errors.New("check is not supported for this request") + ErrorUnsupportedCheck = errors.New("check is not supported for this request") //nolint:errname // ErrorCheckRuntime indicates an individual check had a runtime error. - ErrorCheckRuntime = errors.New("check runtime error") + ErrorCheckRuntime = errors.New("check runtime error") //nolint:errname ) // WithMessage wraps any of the errors listed above.