gitlab.com/evatix-go/core@v1.3.55/coredata/coreonce/ErrorOnce.go (about) 1 package coreonce 2 3 import ( 4 "encoding/json" 5 "errors" 6 7 "gitlab.com/evatix-go/core/constants" 8 "gitlab.com/evatix-go/core/converters" 9 ) 10 11 type ErrorOnce struct { 12 innerData error 13 initializerFunc func() error 14 isInitialized bool 15 } 16 17 func NewErrorOnce(initializerFunc func() error) ErrorOnce { 18 return ErrorOnce{ 19 initializerFunc: initializerFunc, 20 } 21 } 22 23 func NewErrorOncePtr(initializerFunc func() error) *ErrorOnce { 24 return &ErrorOnce{ 25 initializerFunc: initializerFunc, 26 } 27 } 28 29 func (it *ErrorOnce) MarshalJSON() ([]byte, error) { 30 if it.IsNullOrEmpty() { 31 return json.Marshal("") 32 } 33 34 return json.Marshal(it.Value().Error()) 35 } 36 37 func (it *ErrorOnce) UnmarshalJSON(data []byte) error { 38 it.isInitialized = true 39 var str string 40 41 err := json.Unmarshal(data, &str) 42 it.innerData = errors.New(str) 43 44 return err 45 } 46 47 func (it *ErrorOnce) HasError() bool { 48 return !it.IsNullOrEmpty() 49 } 50 51 func (it *ErrorOnce) IsEmpty() bool { 52 return it.IsNullOrEmpty() 53 } 54 55 func (it *ErrorOnce) IsEmptyError() bool { 56 return it.IsNullOrEmpty() 57 } 58 59 func (it *ErrorOnce) HasAnyItem() bool { 60 return !it.IsNullOrEmpty() 61 } 62 63 func (it *ErrorOnce) IsDefined() bool { 64 return !it.IsNullOrEmpty() 65 } 66 67 // IsInvalid 68 // 69 // represents has error 70 func (it *ErrorOnce) IsInvalid() bool { 71 return !it.IsNullOrEmpty() 72 } 73 74 // IsValid 75 // 76 // represents empty error 77 func (it *ErrorOnce) IsValid() bool { 78 return it.IsNullOrEmpty() 79 } 80 81 // IsSuccess 82 // 83 // represents empty error 84 func (it *ErrorOnce) IsSuccess() bool { 85 return it.IsNullOrEmpty() 86 } 87 88 // IsFailed 89 // 90 // represents has error 91 func (it *ErrorOnce) IsFailed() bool { 92 return !it.IsNullOrEmpty() 93 } 94 95 func (it *ErrorOnce) IsNull() bool { 96 return it.Value() == nil 97 } 98 99 func (it *ErrorOnce) IsNullOrEmpty() bool { 100 err := it.Value() 101 102 return err == nil || err.Error() == "" 103 } 104 105 func (it *ErrorOnce) Message() string { 106 if it.IsNull() { 107 return constants.EmptyString 108 } 109 110 return it.Value().Error() 111 } 112 113 func (it *ErrorOnce) IsMessageEqual(msg string) bool { 114 if it.IsNull() { 115 return false 116 } 117 118 return it.Message() == msg 119 } 120 121 // HandleError with panic if error exist or else skip 122 // 123 // Skip if no error type (NoError). 124 func (it *ErrorOnce) HandleError() { 125 if it.IsNullOrEmpty() { 126 return 127 } 128 129 panic(it.Value()) 130 } 131 132 // HandleErrorWith by concatenating message and then panic if error exist or else skip 133 // 134 // Skip if no error type (NoError). 135 func (it *ErrorOnce) HandleErrorWith(messages ...string) { 136 if it.IsNullOrEmpty() { 137 return 138 } 139 140 panic(it.ConcatNewString(messages...)) 141 } 142 143 func (it *ErrorOnce) ConcatNewString(messages ...string) string { 144 additionalMessages := 145 converters.StringsTo.Csv( 146 false, 147 messages..., 148 ) 149 150 if it.IsNullOrEmpty() { 151 return additionalMessages 152 } 153 154 return it.Value().Error() + 155 constants.NewLineUnix + 156 additionalMessages 157 } 158 159 func (it *ErrorOnce) ConcatNew(messages ...string) error { 160 return errors.New(it.ConcatNewString(messages...)) 161 } 162 163 func (it *ErrorOnce) Value() error { 164 if it.isInitialized { 165 return it.innerData 166 } 167 168 it.innerData = it.initializerFunc() 169 it.isInitialized = true 170 171 return it.innerData 172 } 173 174 func (it *ErrorOnce) Execute() error { 175 return it.Value() 176 } 177 178 func (it *ErrorOnce) String() string { 179 return it.Value().Error() 180 } 181 182 func (it *ErrorOnce) Serialize() ([]byte, error) { 183 value := it.Value() 184 185 return json.Marshal(value) 186 }