From 92f17e94939bf8c213419749f5f7b48d2f0e618c Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 4 Dec 2023 22:22:18 +0100 Subject: [PATCH 1/2] Add errors.Join wrapper Signed-off-by: Evan Lezar --- internal/errors/errors.go | 27 +++++++++++++++++++++++ internal/errors/errors_legacy.go | 36 +++++++++++++++++++++++++++++++ internal/errors/errors_modern.go | 28 ++++++++++++++++++++++++ internal/lookup/merge.go | 8 +++---- internal/modifier/cdi/registry.go | 2 +- internal/runtime/logger.go | 2 +- internal/runtime/runtime.go | 2 +- 7 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 internal/errors/errors.go create mode 100644 internal/errors/errors_legacy.go create mode 100644 internal/errors/errors_modern.go diff --git a/internal/errors/errors.go b/internal/errors/errors.go new file mode 100644 index 000000000..937f52e6a --- /dev/null +++ b/internal/errors/errors.go @@ -0,0 +1,27 @@ +/** +# Copyright 2023 NVIDIA CORPORATION +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +**/ + +package errors + +import ( + "errors" +) + +// New provides a wrapper for errors.New. +// This allows this internal package to be used as a drop-in replacement for the stdlib package. +func New(text string) error { + return errors.New(text) +} diff --git a/internal/errors/errors_legacy.go b/internal/errors/errors_legacy.go new file mode 100644 index 000000000..54c46ecfc --- /dev/null +++ b/internal/errors/errors_legacy.go @@ -0,0 +1,36 @@ +//go:build !go1.20 + +/** +# Copyright 2023 NVIDIA CORPORATION +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +**/ + +package errors + +import "fmt" + +// Join provides a legacy implementation for errors.Join. +func Join(errs ...error) error { + var all error + for _, err := range errs { + if err == nil { + continue + } + if all == nil { + all = err + } + all = fmt.Errorf("%v %w", all, err) + } + return all +} diff --git a/internal/errors/errors_modern.go b/internal/errors/errors_modern.go new file mode 100644 index 000000000..b2c2d414b --- /dev/null +++ b/internal/errors/errors_modern.go @@ -0,0 +1,28 @@ +//go:build go1.20 + +/** +# Copyright 2023 NVIDIA CORPORATION +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +**/ + +package errors + +import ( + "errors" +) + +// Join wraps errors.Join for newer golang versions. +func Join(errs ...error) error { + return errors.Join(errs...) +} diff --git a/internal/lookup/merge.go b/internal/lookup/merge.go index fa20b5125..df202033d 100644 --- a/internal/lookup/merge.go +++ b/internal/lookup/merge.go @@ -16,7 +16,7 @@ package lookup -import "errors" +import "github.com/NVIDIA/nvidia-container-toolkit/internal/errors" type first []Locator @@ -34,14 +34,14 @@ func First(locators ...Locator) Locator { // Locate returns the results for the first locator that returns a non-empty non-error result. func (f first) Locate(pattern string) ([]string, error) { - var allErrors []error + var allErrors error for _, l := range f { if l == nil { continue } candidates, err := l.Locate(pattern) if err != nil { - allErrors = append(allErrors, err) + allErrors = errors.Join(allErrors, err) continue } if len(candidates) > 0 { @@ -49,5 +49,5 @@ func (f first) Locate(pattern string) ([]string, error) { } } - return nil, errors.Join(allErrors...) + return nil, allErrors } diff --git a/internal/modifier/cdi/registry.go b/internal/modifier/cdi/registry.go index d1faffad5..024db4d5e 100644 --- a/internal/modifier/cdi/registry.go +++ b/internal/modifier/cdi/registry.go @@ -17,12 +17,12 @@ package cdi import ( - "errors" "fmt" "github.com/opencontainers/runtime-spec/specs-go" "tags.cncf.io/container-device-interface/pkg/cdi" + "github.com/NVIDIA/nvidia-container-toolkit/internal/errors" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/internal/oci" ) diff --git a/internal/runtime/logger.go b/internal/runtime/logger.go index 1b9500265..bce78e156 100644 --- a/internal/runtime/logger.go +++ b/internal/runtime/logger.go @@ -17,7 +17,6 @@ package runtime import ( - "errors" "fmt" "io" "os" @@ -28,6 +27,7 @@ import ( "github.com/sirupsen/logrus" + "github.com/NVIDIA/nvidia-container-toolkit/internal/errors" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" ) diff --git a/internal/runtime/runtime.go b/internal/runtime/runtime.go index c98f96581..c7fe7594e 100644 --- a/internal/runtime/runtime.go +++ b/internal/runtime/runtime.go @@ -18,13 +18,13 @@ package runtime import ( "encoding/json" - "errors" "fmt" "strings" "github.com/opencontainers/runtime-spec/specs-go" "github.com/NVIDIA/nvidia-container-toolkit/internal/config" + "github.com/NVIDIA/nvidia-container-toolkit/internal/errors" "github.com/NVIDIA/nvidia-container-toolkit/internal/info" ) From f23fd2ce38ee3a9e87ac41c265b637cf97990ac7 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 4 Dec 2023 23:13:42 +0100 Subject: [PATCH 2/2] Fix double error wrap fmt Signed-off-by: Evan Lezar --- cmd/nvidia-ctk/config/config.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/nvidia-ctk/config/config.go b/cmd/nvidia-ctk/config/config.go index 3f10f7276..5a21b6886 100644 --- a/cmd/nvidia-ctk/config/config.go +++ b/cmd/nvidia-ctk/config/config.go @@ -17,7 +17,6 @@ package config import ( - "errors" "fmt" "reflect" "strconv" @@ -28,6 +27,7 @@ import ( createdefault "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/config/create-default" "github.com/NVIDIA/nvidia-container-toolkit/cmd/nvidia-ctk/config/flags" "github.com/NVIDIA/nvidia-container-toolkit/internal/config" + "github.com/NVIDIA/nvidia-container-toolkit/internal/errors" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" ) @@ -141,7 +141,7 @@ func setFlagToKeyValue(setFlag string) (string, interface{}, error) { field, err := getField(key) if err != nil { - return key, nil, fmt.Errorf("%w: %w", errInvalidConfigOption, err) + return key, nil, errors.Join(err, errInvalidConfigOption) } kind := field.Kind() @@ -157,7 +157,7 @@ func setFlagToKeyValue(setFlag string) (string, interface{}, error) { case reflect.Bool: b, err := strconv.ParseBool(value) if err != nil { - return key, value, fmt.Errorf("%w: %w", errInvalidFormat, err) + return key, value, errors.Join(err, errInvalidFormat) } return key, b, err case reflect.String: @@ -172,7 +172,7 @@ func setFlagToKeyValue(setFlag string) (string, interface{}, error) { for _, v := range valueParts { vi, err := strconv.ParseInt(v, 10, 0) if err != nil { - return key, nil, fmt.Errorf("%w: %w", errInvalidFormat, err) + return key, nil, errors.Join(err, errInvalidFormat) } output = append(output, vi) }