-
Notifications
You must be signed in to change notification settings - Fork 6
/
errorUtils.go
243 lines (219 loc) · 6.11 KB
/
errorUtils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package utils
import (
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/trimble-oss/tierceron/pkg/core"
)
var headlessService bool
func init() {
headlessService = false
}
func InitHeadless(headless bool) {
headlessService = headless
}
// CheckError Simplifies the error checking process
func CheckError(config *core.CoreConfig, err error, exit bool) {
// If code wants to exit and ExitOnFailure is specified,
// then we can exit here.
if err != nil && exit && config.ExitOnFailure {
log.Fatal(err)
}
}
// CheckErrorNoStack Simplifies the error checking process
func CheckErrorNoStack(config *core.CoreConfig, err error, exit bool) {
if err != nil {
if !headlessService {
fmt.Println(err)
}
if exit && config.ExitOnFailure {
os.Exit(1)
}
}
}
// CheckWarnings Checks warnings returned from various vault relation operations
func CheckWarning(config *core.CoreConfig, warning string, exit bool) {
if len(warning) > 0 {
if !headlessService {
fmt.Println(warning)
}
if exit && config.ExitOnFailure {
os.Exit(1)
}
}
}
// CheckWarnings Checks warnings returned from various vault relation operations
func CheckWarnings(config *core.CoreConfig, warnings []string, exit bool) {
if len(warnings) > 0 {
if !headlessService {
for _, w := range warnings {
fmt.Println(w)
}
}
if exit && config.ExitOnFailure {
os.Exit(1)
}
}
}
// LogError Writes error to the log file and terminates if an error occurs
func LogError(config *core.CoreConfig, err error, f *os.File, exit bool) {
if err != nil {
_prefix := log.Prefix()
log.SetOutput(f)
log.SetPrefix("[ERROR]")
if exit && config.ExitOnFailure {
if !headlessService {
fmt.Printf("Errors encountered, exiting and writing to log file: %s\n", f.Name())
log.Fatal(err)
} else {
os.Exit(-1)
}
} else {
if !headlessService {
log.Println(err)
}
log.SetPrefix(_prefix)
}
}
}
// LogWarnings Writes array of warnings to the log file and terminates
func LogWarnings(config *core.CoreConfig, warnings []string, f *os.File, exit bool) {
if len(warnings) > 0 {
_prefix := log.Prefix()
log.SetOutput(f)
log.SetPrefix("[WARNS]")
if !headlessService {
for _, w := range warnings {
log.Println(w)
}
}
if exit && config.ExitOnFailure {
if !headlessService {
fmt.Printf("Warnings encountered, exiting and writing to log file: %s\n", f.Name())
}
os.Exit(1)
} else {
log.SetPrefix(_prefix)
}
}
}
// LogErrorObject writes errors to the passed logger object and exits
func LogWarningMessage(config *core.CoreConfig, errorMessage string, exit bool) {
_prefix := config.Log.Prefix()
config.Log.SetPrefix("[WARN]")
if exit && config.ExitOnFailure {
if !headlessService {
fmt.Printf("Errors encountered, exiting and writing to log file\n")
}
config.Log.Fatal(errorMessage)
} else {
config.Log.Println(errorMessage)
config.Log.SetPrefix(_prefix)
}
}
// LogErrorObject writes errors to the passed logger object and exits
func LogMessageErrorObject(config *core.CoreConfig, errorMessage string, err error, exit bool) {
if err != nil {
_prefix := config.Log.Prefix()
config.Log.SetPrefix("[ERROR]")
if exit && config.ExitOnFailure {
if !headlessService {
fmt.Printf("Errors encountered, exiting and writing to log file: %v\n", err)
}
config.Log.Fatal(err)
} else {
if errorMessage != "" {
config.Log.Println(errorMessage)
}
config.Log.Println(err)
config.Log.SetPrefix(_prefix)
}
}
}
// LogErrorObject writes errors to the passed logger object and exits
func LogErrorMessage(config *core.CoreConfig, errorMessage string, exit bool) {
_prefix := config.Log.Prefix()
config.Log.SetPrefix("[ERROR]")
if exit && config.ExitOnFailure {
if !headlessService {
fmt.Printf("Errors encountered, exiting and writing to log file\n")
}
config.Log.Fatal(errorMessage)
} else {
config.Log.Println(errorMessage)
config.Log.SetPrefix(_prefix)
}
}
// LogErrorObject writes errors to the passed logger object and exits
func LogErrorObject(config *core.CoreConfig, err error, exit bool) {
if err != nil {
_prefix := config.Log.Prefix()
config.Log.SetPrefix("[ERROR]")
if exit && config.ExitOnFailure {
if !headlessService {
fmt.Printf("Errors encountered, exiting and writing to log file: %v\n", err)
}
config.Log.Fatal(SanitizeForLogging(err.Error()))
} else {
config.Log.Println(SanitizeForLogging(err.Error()))
config.Log.SetPrefix(_prefix)
}
}
}
// LogErrorObject writes errors to the passed logger object and exits
func LogInfo(config *core.CoreConfig, msg string) {
if !headlessService {
fmt.Println(SanitizeForLogging(msg))
}
if config != nil && config.Log != nil {
_prefix := config.Log.Prefix()
config.Log.SetPrefix("[INFO]")
config.Log.Println(SanitizeForLogging(msg))
config.Log.SetPrefix(_prefix)
}
}
// LogWarningsObject writes warnings to the passed logger object and exits
func LogWarningsObject(config *core.CoreConfig, warnings []string, exit bool) {
if len(warnings) > 0 {
_prefix := config.Log.Prefix()
config.Log.SetPrefix("[WARNS]")
for _, w := range warnings {
config.Log.Println(w)
}
if exit && config.ExitOnFailure {
if !headlessService {
fmt.Println("Warnings encountered, exiting and writing to log file")
}
os.Exit(1)
} else {
config.Log.SetPrefix(_prefix)
}
}
}
// LogAndSafeExit -- provides isolated location of os.Exit to ensure os.Exit properly processed.
func LogAndSafeExit(config *core.CoreConfig, message string, code int) error {
if config.Log != nil && message != "" {
LogInfo(config, message)
}
if config.ExitOnFailure {
os.Exit(code)
}
return errors.New(message)
}
// LogErrorAndSafeExit -- provides isolated location of os.Exit to ensure os.Exit properly processed.
func LogErrorAndSafeExit(config *core.CoreConfig, err error, code int) error {
if config.Log != nil && err != nil {
LogInfo(config, err.Error())
}
if err != nil && config.ExitOnFailure {
os.Exit(code)
}
return err
}
func SanitizeForLogging(errMsg string) string {
errMsgSanitized := strings.ReplaceAll(errMsg, "\n", "")
errMsgSanitized = strings.ReplaceAll(errMsgSanitized, "\r", "")
return errMsgSanitized
}