Add errors.Join wrapper

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-12-04 22:22:18 +01:00
parent 15f609a52d
commit 92f17e9493
7 changed files with 98 additions and 7 deletions

27
internal/errors/errors.go Normal file
View File

@ -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)
}

View File

@ -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
}

View File

@ -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...)
}

View File

@ -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
}

View File

@ -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"
)

View File

@ -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"
)

View File

@ -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"
)