github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/k8s.io/kubernetes/pkg/runtime/error.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors All rights reserved.
     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  
    23  	"k8s.io/kubernetes/pkg/api/unversioned"
    24  )
    25  
    26  type notRegisteredErr struct {
    27  	gvk unversioned.GroupVersionKind
    28  	t   reflect.Type
    29  }
    30  
    31  // NewNotRegisteredErr is exposed for testing.
    32  func NewNotRegisteredErr(gvk unversioned.GroupVersionKind, t reflect.Type) error {
    33  	return &notRegisteredErr{gvk: gvk, t: t}
    34  }
    35  
    36  func (k *notRegisteredErr) Error() string {
    37  	if k.t != nil {
    38  		return fmt.Sprintf("no kind is registered for the type %v", k.t)
    39  	}
    40  	if len(k.gvk.Kind) == 0 {
    41  		return fmt.Sprintf("no version %q has been registered", k.gvk.GroupVersion())
    42  	}
    43  	if k.gvk.Version == APIVersionInternal {
    44  		return fmt.Sprintf("no kind %q is registered for the internal version of group %q", k.gvk.Kind, k.gvk.Group)
    45  	}
    46  
    47  	return fmt.Sprintf("no kind %q is registered for version %q", k.gvk.Kind, k.gvk.GroupVersion())
    48  }
    49  
    50  // IsNotRegisteredError returns true if the error indicates the provided
    51  // object or input data is not registered.
    52  func IsNotRegisteredError(err error) bool {
    53  	if err == nil {
    54  		return false
    55  	}
    56  	_, ok := err.(*notRegisteredErr)
    57  	return ok
    58  }
    59  
    60  type missingKindErr struct {
    61  	data string
    62  }
    63  
    64  func NewMissingKindErr(data string) error {
    65  	return &missingKindErr{data}
    66  }
    67  
    68  func (k *missingKindErr) Error() string {
    69  	return fmt.Sprintf("Object 'Kind' is missing in '%s'", k.data)
    70  }
    71  
    72  // IsMissingKind returns true if the error indicates that the provided object
    73  // is missing a 'Kind' field.
    74  func IsMissingKind(err error) bool {
    75  	if err == nil {
    76  		return false
    77  	}
    78  	_, ok := err.(*missingKindErr)
    79  	return ok
    80  }
    81  
    82  type missingVersionErr struct {
    83  	data string
    84  }
    85  
    86  // IsMissingVersion returns true if the error indicates that the provided object
    87  // is missing a 'Versioj' field.
    88  func NewMissingVersionErr(data string) error {
    89  	return &missingVersionErr{data}
    90  }
    91  
    92  func (k *missingVersionErr) Error() string {
    93  	return fmt.Sprintf("Object 'apiVersion' is missing in '%s'", k.data)
    94  }
    95  
    96  func IsMissingVersion(err error) bool {
    97  	if err == nil {
    98  		return false
    99  	}
   100  	_, ok := err.(*missingVersionErr)
   101  	return ok
   102  }