Add errdefs package
Providers should use this package so the virtual kubelet core controllers can understand the errors produced from the provider code.
This commit is contained in:
65
errdefs/notfound.go
Normal file
65
errdefs/notfound.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package errdefs
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// NotFound is an error interface which denotes whether the opration failed due
|
||||
// to a the resource not being found.
|
||||
type ErrNotFound interface {
|
||||
NotFound() bool
|
||||
error
|
||||
}
|
||||
|
||||
type notFoundError struct {
|
||||
error
|
||||
}
|
||||
|
||||
func (e *notFoundError) NotFound() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (e *notFoundError) Cause() error {
|
||||
return e.error
|
||||
}
|
||||
|
||||
// AsNotFound wraps the passed in error to make it of type ErrNotFound
|
||||
//
|
||||
// Callers should make sure the passed in error has exactly the error message
|
||||
// it wants as this function does not decorate the message.
|
||||
func AsNotFound(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
return ¬FoundError{err}
|
||||
}
|
||||
|
||||
// NotFound makes an ErrNotFound from the provided error message
|
||||
func NotFound(msg string) error {
|
||||
return ¬FoundError{errors.New(msg)}
|
||||
}
|
||||
|
||||
// NotFoundf makes an ErrNotFound from the provided error format and args
|
||||
func NotFoundf(format string, args ...interface{}) error {
|
||||
return ¬FoundError{fmt.Errorf(format, args...)}
|
||||
}
|
||||
|
||||
// IsNotFound determines if the passed in error is of type ErrNotFound
|
||||
//
|
||||
// This will traverse the causal chain (`Cause() error`), until it finds an error
|
||||
// which implements the `NotFound` interface.
|
||||
func IsNotFound(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
if e, ok := err.(ErrNotFound); ok {
|
||||
return e.NotFound()
|
||||
}
|
||||
|
||||
if e, ok := err.(causal); ok {
|
||||
return IsNotFound(e)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user