github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controllerutil/errors.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package controllerutil 21 22 import ( 23 "errors" 24 "fmt" 25 ) 26 27 type Error struct { 28 Type ErrorType 29 Message string 30 } 31 32 var _ error = &Error{} 33 34 // Error implements the error interface. 35 func (v *Error) Error() string { 36 return v.Message 37 } 38 39 // ErrorType is explicit error type. 40 type ErrorType string 41 42 const ( 43 // ErrorWaitCacheRefresh waits for synchronization of the corresponding object cache in client-go from ApiServer. 44 ErrorWaitCacheRefresh ErrorType = "WaitCacheRefresh" 45 // ErrorTypeNotFound not found any resource. 46 ErrorTypeNotFound ErrorType = "NotFound" 47 48 ErrorTypeRequeue ErrorType = "Requeue" // requeue for reconcile. 49 50 ErrorTypeFatal ErrorType = "Fatal" // fatal error 51 52 // ErrorType for cluster controller 53 ErrorTypeBackupFailed ErrorType = "BackupFailed" 54 ErrorTypeRestoreFailed ErrorType = "RestoreFailed" 55 ErrorTypeNeedWaiting ErrorType = "NeedWaiting" // waiting for next reconcile 56 57 // ErrorType for preflight 58 ErrorTypePreflightCommon = "PreflightCommon" 59 ErrorTypeSkipPreflight = "SkipPreflight" 60 ) 61 62 var ErrFailedToAddFinalizer = errors.New("failed to add finalizer") 63 64 func NewError(errorType ErrorType, message string) *Error { 65 return &Error{ 66 Type: errorType, 67 Message: message, 68 } 69 } 70 71 func NewErrorf(errorType ErrorType, format string, a ...any) *Error { 72 return &Error{ 73 Type: errorType, 74 Message: fmt.Sprintf(format, a...), 75 } 76 } 77 78 // IsTargetError checks if the error is the target error. 79 func IsTargetError(err error, errorType ErrorType) bool { 80 if tmpErr, ok := err.(*Error); ok || errors.As(err, &tmpErr) { 81 return tmpErr.Type == errorType 82 } 83 return false 84 } 85 86 // UnwrapControllerError unwraps the Controller error from target error. 87 func UnwrapControllerError(err error) *Error { 88 if tmpErr, ok := err.(*Error); ok || errors.As(err, &tmpErr) { 89 return tmpErr 90 } 91 return nil 92 } 93 94 // NewNotFound returns a new Error with ErrorTypeNotFound. 95 func NewNotFound(format string, a ...any) *Error { 96 return &Error{ 97 Type: ErrorTypeNotFound, 98 Message: fmt.Sprintf(format, a...), 99 } 100 } 101 102 // IsNotFound returns true if the specified error is the error type of ErrorTypeNotFound. 103 func IsNotFound(err error) bool { 104 return IsTargetError(err, ErrorTypeNotFound) 105 } 106 107 // NewFatalError returns a new Error with ErrorTypeFatal 108 func NewFatalError(message string) *Error { 109 return NewErrorf(ErrorTypeFatal, message) 110 }