mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2024-11-22 08:18:32 +00:00
Add errors.Join wrapper
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
parent
15f609a52d
commit
92f17e9493
27
internal/errors/errors.go
Normal file
27
internal/errors/errors.go
Normal 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)
|
||||
}
|
36
internal/errors/errors_legacy.go
Normal file
36
internal/errors/errors_legacy.go
Normal 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
|
||||
}
|
28
internal/errors/errors_modern.go
Normal file
28
internal/errors/errors_modern.go
Normal 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...)
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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"
|
||||
)
|
||||
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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"
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user