errors: use cpuguy83/strongerrors
Signed-off-by: Paulo Pires <pjpires@gmail.com>
This commit is contained in:
49
errors/errors.go
Normal file
49
errors/errors.go
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright © 2017 The virtual-kubelet authors
|
||||
//
|
||||
// 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"
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
)
|
||||
|
||||
// DeadlineWithMessage creates a "Deadline" error with the specified message and formatting arguments.
|
||||
func DeadlineWithMessage(format string, args ...interface{}) error {
|
||||
return strongerrors.Deadline(fmt.Errorf(format, args...))
|
||||
}
|
||||
|
||||
// Exhausted creates an "Exhausted" error from the specified error.
|
||||
func Exhausted(err error) error {
|
||||
return strongerrors.Exhausted(err)
|
||||
}
|
||||
|
||||
// InvalidArgument creates an "InvalidArgument" error from the specified error.
|
||||
func InvalidArgument(err error) error {
|
||||
return strongerrors.InvalidArgument(err)
|
||||
}
|
||||
|
||||
// InvalidArgumentWithMessage creates an "InvalidArgument" error with the specified message and formatting arguments.
|
||||
func InvalidArgumentWithMessage(format string, args ...interface{}) error {
|
||||
return strongerrors.InvalidArgument(fmt.Errorf(format, args...))
|
||||
}
|
||||
|
||||
// Unknown creates an "Unknown" error from the specified error and optional message and formatting arguments.
|
||||
func Unknown(err error, args ...interface{}) error {
|
||||
// If we've been given additional arguments, re-format the error
|
||||
if len(args) >= 1 {
|
||||
err = fmt.Errorf("%s: %s", fmt.Sprintf(args[0].(string), args[1:]...), err)
|
||||
}
|
||||
return strongerrors.Unknown(err)
|
||||
}
|
||||
73
errors/errors_test.go
Normal file
73
errors/errors_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright © 2017 The virtual-kubelet authors
|
||||
//
|
||||
// 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 (
|
||||
goerrors "errors"
|
||||
"testing"
|
||||
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
)
|
||||
|
||||
func TestUnknownFromRoot(t *testing.T) {
|
||||
// Create a root cause.
|
||||
// This represents an error returned by a function invocation.
|
||||
err1 := goerrors.New("root cause")
|
||||
// Create an error of type "Unknown" with err1 as the cause and containing an additional message.
|
||||
err2 := Unknown(err1)
|
||||
|
||||
// Make sure that the resulting error is indeed of type "Unknown".
|
||||
if !strongerrors.IsUnknown(err2) {
|
||||
t.Fatal("expected err2 to be of type Unknown")
|
||||
}
|
||||
// Make sure that the error's message is equal to the root cause's message.
|
||||
if err2.Error() != "root cause" {
|
||||
t.Fatal("expected err2's error message to be equal to the root cause's message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownFromRootCauseWithMessage(t *testing.T) {
|
||||
// Create a root cause.
|
||||
// This represents an error returned by a function invocation.
|
||||
err1 := goerrors.New("root cause")
|
||||
// Create an error of type "Unknown" with err1 as the cause and containing an additional message.
|
||||
err2 := Unknown(err1, "extra info")
|
||||
|
||||
// Make sure that the resulting error is indeed of type "Unknown".
|
||||
if !strongerrors.IsUnknown(err2) {
|
||||
t.Fatal("expected err2 to be of type Unknown")
|
||||
}
|
||||
// Make sure that the message was adequately formatted.
|
||||
if err2.Error() != "extra info: root cause" {
|
||||
t.Fatal("expected err2 to be adequately formatted as a string")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownFromRootCauseWithMessageAndFormattingParameters(t *testing.T) {
|
||||
// Create a root cause.
|
||||
// This represents an error returned by a function invocation.
|
||||
err1 := goerrors.New("root cause")
|
||||
// Create an error of type "Unknown" with err1 as the cause and containing an additional message.
|
||||
err2 := Unknown(err1, "expected %d, got %d", 0, 1)
|
||||
|
||||
// Make sure that the resulting error is indeed of type "Unknown".
|
||||
if !strongerrors.IsUnknown(err2) {
|
||||
t.Fatal("expected err2 to be of type Unknown")
|
||||
}
|
||||
// Make sure that the message was adequately formatted.
|
||||
if err2.Error() != "expected 0, got 1: root cause" {
|
||||
t.Fatal("expected err2 to be adequately formatted as a string")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user