From 92f17e94939bf8c213419749f5f7b48d2f0e618c Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 4 Dec 2023 22:22:18 +0100 Subject: [PATCH] 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 00000000..937f52e6 --- /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 00000000..54c46ecf --- /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 00000000..b2c2d414 --- /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 fa20b512..df202033 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 d1faffad..024db4d5 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 1b950026..bce78e15 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 c98f9658..c7fe7594 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" )