github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/error.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package runtime
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"strings"
    23  
    24  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    25  )
    26  
    27  type notRegisteredErr struct {
    28  	schemeName string
    29  	gvk        schema.GroupVersionKind
    30  	target     GroupVersioner
    31  	t          reflect.Type
    32  }
    33  
    34  func NewNotRegisteredErrForKind(schemeName string, gvk schema.GroupVersionKind) error {
    35  	return &notRegisteredErr{schemeName: schemeName, gvk: gvk}
    36  }
    37  
    38  func NewNotRegisteredErrForType(schemeName string, t reflect.Type) error {
    39  	return &notRegisteredErr{schemeName: schemeName, t: t}
    40  }
    41  
    42  func NewNotRegisteredErrForTarget(schemeName string, t reflect.Type, target GroupVersioner) error {
    43  	return &notRegisteredErr{schemeName: schemeName, t: t, target: target}
    44  }
    45  
    46  func NewNotRegisteredGVKErrForTarget(schemeName string, gvk schema.GroupVersionKind, target GroupVersioner) error {
    47  	return &notRegisteredErr{schemeName: schemeName, gvk: gvk, target: target}
    48  }
    49  
    50  func (k *notRegisteredErr) Error() string {
    51  	if k.t != nil && k.target != nil {
    52  		return fmt.Sprintf("%v is not suitable for converting to %q in scheme %q", k.t, k.target, k.schemeName)
    53  	}
    54  	nullGVK := schema.GroupVersionKind{}
    55  	if k.gvk != nullGVK && k.target != nil {
    56  		return fmt.Sprintf("%q is not suitable for converting to %q in scheme %q", k.gvk.GroupVersion(), k.target, k.schemeName)
    57  	}
    58  	if k.t != nil {
    59  		return fmt.Sprintf("no kind is registered for the type %v in scheme %q", k.t, k.schemeName)
    60  	}
    61  	if len(k.gvk.Kind) == 0 {
    62  		return fmt.Sprintf("no version %q has been registered in scheme %q", k.gvk.GroupVersion(), k.schemeName)
    63  	}
    64  	if k.gvk.Version == APIVersionInternal {
    65  		return fmt.Sprintf("no kind %q is registered for the internal version of group %q in scheme %q", k.gvk.Kind, k.gvk.Group, k.schemeName)
    66  	}
    67  
    68  	return fmt.Sprintf("no kind %q is registered for version %q in scheme %q", k.gvk.Kind, k.gvk.GroupVersion(), k.schemeName)
    69  }
    70  
    71  // IsNotRegisteredError returns true if the error indicates the provided
    72  // object or input data is not registered.
    73  func IsNotRegisteredError(err error) bool {
    74  	if err == nil {
    75  		return false
    76  	}
    77  	_, ok := err.(*notRegisteredErr)
    78  	return ok
    79  }
    80  
    81  type missingKindErr struct {
    82  	data string
    83  }
    84  
    85  func NewMissingKindErr(data string) error {
    86  	return &missingKindErr{data}
    87  }
    88  
    89  func (k *missingKindErr) Error() string {
    90  	return fmt.Sprintf("Object 'Kind' is missing in '%s'", k.data)
    91  }
    92  
    93  // IsMissingKind returns true if the error indicates that the provided object
    94  // is missing a 'Kind' field.
    95  func IsMissingKind(err error) bool {
    96  	if err == nil {
    97  		return false
    98  	}
    99  	_, ok := err.(*missingKindErr)
   100  	return ok
   101  }
   102  
   103  type missingVersionErr struct {
   104  	data string
   105  }
   106  
   107  func NewMissingVersionErr(data string) error {
   108  	return &missingVersionErr{data}
   109  }
   110  
   111  func (k *missingVersionErr) Error() string {
   112  	return fmt.Sprintf("Object 'apiVersion' is missing in '%s'", k.data)
   113  }
   114  
   115  // IsMissingVersion returns true if the error indicates that the provided object
   116  // is missing a 'Version' field.
   117  func IsMissingVersion(err error) bool {
   118  	if err == nil {
   119  		return false
   120  	}
   121  	_, ok := err.(*missingVersionErr)
   122  	return ok
   123  }
   124  
   125  // strictDecodingError is a base error type that is returned by a strict Decoder such
   126  // as UniversalStrictDecoder.
   127  type strictDecodingError struct {
   128  	errors []error
   129  }
   130  
   131  // NewStrictDecodingError creates a new strictDecodingError object.
   132  func NewStrictDecodingError(errors []error) error {
   133  	return &strictDecodingError{
   134  		errors: errors,
   135  	}
   136  }
   137  
   138  func (e *strictDecodingError) Error() string {
   139  	var s strings.Builder
   140  	s.WriteString("strict decoding error: ")
   141  	for i, err := range e.errors {
   142  		if i != 0 {
   143  			s.WriteString(", ")
   144  		}
   145  		s.WriteString(err.Error())
   146  	}
   147  	return s.String()
   148  }
   149  
   150  func (e *strictDecodingError) Errors() []error {
   151  	return e.errors
   152  }
   153  
   154  // IsStrictDecodingError returns true if the error indicates that the provided object
   155  // strictness violations.
   156  func IsStrictDecodingError(err error) bool {
   157  	if err == nil {
   158  		return false
   159  	}
   160  	_, ok := err.(*strictDecodingError)
   161  	return ok
   162  }
   163  
   164  // AsStrictDecodingError returns a strict decoding error
   165  // containing all the strictness violations.
   166  func AsStrictDecodingError(err error) (*strictDecodingError, bool) {
   167  	if err == nil {
   168  		return nil, false
   169  	}
   170  	strictErr, ok := err.(*strictDecodingError)
   171  	return strictErr, ok
   172  }