@@ -19,45 +19,15 @@ import (
1919 "strings"
2020)
2121
22+ const pathDelimitor = "."
23+
2224// ErrMissingFnConfig raises error if a required functionConfig is missing.
2325type ErrMissingFnConfig struct {}
2426
2527func (ErrMissingFnConfig ) Error () string {
2628 return "unable to find the functionConfig in the resourceList"
2729}
2830
29- // errKubeObjectFields raises if the KubeObject operation panics.
30- type errKubeObjectFields struct {
31- obj * KubeObject
32- fields []string
33- }
34-
35- func (e * errKubeObjectFields ) Error () string {
36- return fmt .Sprintf ("Resource(apiVersion=%v, kind=%v, Name=%v) has unmatched field type: `%v" ,
37- e .obj .GetAPIVersion (), e .obj .GetKind (), e .obj .GetName (), strings .Join (e .fields , "/" ))
38- }
39-
40- // errSubObjectFields raises if the SubObject operation panics.
41- type errSubObjectFields struct {
42- fields []string
43- }
44-
45- func (e * errSubObjectFields ) Error () string {
46- return fmt .Sprintf ("SubObject has unmatched field type: `%v" , strings .Join (e .fields , "/" ))
47- }
48-
49- type errResultEnd struct {
50- obj * KubeObject
51- message string
52- }
53-
54- func (e * errResultEnd ) Error () string {
55- if e .obj != nil {
56- return fmt .Sprintf ("function is terminated by %v: %v" , e .obj .ShortString (), e .message )
57- }
58- return fmt .Sprintf ("function is terminated: %v" , e .message )
59- }
60-
6131type ErrAttemptToTouchUpstreamIdentifier struct {}
6232
6333func (ErrAttemptToTouchUpstreamIdentifier ) Error () string {
@@ -71,3 +41,25 @@ type ErrInternalAnnotation struct {
7141func (e * ErrInternalAnnotation ) Error () string {
7242 return e .Message
7343}
44+
45+ // NewErrUnmatchedField returns a ErrUnmatchedField error with the specific field path of a KubeObject that has the mismatched data type.
46+ func NewErrUnmatchedField (obj SubObject , fields []string , expectedFieldType any ) * ErrUnmatchedField {
47+ relativefields := strings .Join (fields , pathDelimitor )
48+ obj .fieldpath += pathDelimitor + relativefields
49+ return & ErrUnmatchedField {
50+ SubObject : & obj , DataType : fmt .Sprintf ("%T" , expectedFieldType ),
51+ }
52+ }
53+
54+ // ErrUnmatchedField defines the error when a KubeObject's field paths has a different data type as expected
55+ // e.g. ConfigMap `.data` is string map. If the a ConfigMap KubeObject calls `NestedInt("data")`, this error should raise.
56+ type ErrUnmatchedField struct {
57+ SubObject * SubObject
58+ DataType string
59+ }
60+
61+ // Error returns the message to guide users
62+ func (e * ErrUnmatchedField ) Error () string {
63+ return fmt .Sprintf ("Resource(apiVersion=%v, kind=%v) has unmatched field type %q in fieldpath %v" ,
64+ e .SubObject .parentGVK .GroupVersion (), e .SubObject .parentGVK .Kind , e .DataType , e .SubObject .fieldpath )
65+ }
0 commit comments