knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/reconciler/events.go (about) 1 /* 2 Copyright 2020 The Knative Authors 3 4 Licensed under the Apache License, Veroute.on 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 reconciler 18 19 import ( 20 "errors" 21 "fmt" 22 ) 23 24 // Event leverages go's 1.13 error wrapping. 25 type Event error 26 27 // EventIs reports whether any error in err's chain matches target. 28 // 29 // The chain consists of err itself followed by the sequence of errors obtained by 30 // repeatedly calling Unwrap. 31 // 32 // An error is considered to match a target if it is equal to that target or if 33 // it implements a method Is(error) bool such that Is(target) returns true. 34 // (text from errors/wrap.go) 35 var EventIs = errors.Is 36 37 // EventAs finds the first error in err's chain that matches target, and if so, sets 38 // target to that error value and returns true. 39 // 40 // The chain consists of err itself followed by the sequence of errors obtained by 41 // repeatedly calling Unwrap. 42 // 43 // An error matches target if the error's concrete value is assignable to the value 44 // pointed to by target, or if the error has a method As(interface{}) bool such that 45 // As(target) returns true. In the latter case, the As method is responsible for 46 // setting target. 47 // 48 // As will panic if target is not a non-nil pointer to either a type that implements 49 // error, or to any interface type. As returns false if err is nil. 50 // (text from errors/wrap.go) 51 var EventAs = errors.As 52 53 // NewEvent returns an Event fully populated. 54 func NewEvent(eventtype, reason, messageFmt string, args ...interface{}) Event { 55 return &ReconcilerEvent{ 56 EventType: eventtype, 57 Reason: reason, 58 Format: messageFmt, 59 Args: args, 60 } 61 } 62 63 // ReconcilerEvent wraps the fields required for recorders to create a 64 // kubernetes recorder Event. 65 type ReconcilerEvent struct { //nolint:errname 66 EventType string 67 Reason string 68 Format string 69 Args []interface{} 70 } 71 72 // make sure ReconcilerEvent implements error. 73 var _ error = (*ReconcilerEvent)(nil) 74 75 // Is returns if the target error is a ReconcilerEvent type checking that 76 // EventType and Reason match. 77 func (e *ReconcilerEvent) Is(target error) bool { 78 var t *ReconcilerEvent 79 if errors.As(target, &t) { 80 if t != nil && t.EventType == e.EventType && t.Reason == e.Reason { 81 return true 82 } 83 return false 84 } 85 // Allow for wrapped errors. 86 err := fmt.Errorf(e.Format, e.Args...) 87 return errors.Is(err, target) 88 } 89 90 // As allows ReconcilerEvents to be treated as regular error types. 91 func (e *ReconcilerEvent) As(target interface{}) bool { 92 err := fmt.Errorf(e.Format, e.Args...) 93 return errors.As(err, target) 94 } 95 96 // Error returns the string that is formed by using the format string with the 97 // provided args. 98 func (e *ReconcilerEvent) Error() string { 99 return fmt.Errorf(e.Format, e.Args...).Error() 100 }