-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrprefixdtomechanics.go
426 lines (341 loc) · 9.77 KB
/
errprefixdtomechanics.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package errpref
import (
"fmt"
"strings"
"sync"
)
type errPrefixDtoMechanics struct {
lock *sync.Mutex
}
// ptr - Returns a pointer to a new instance of errPrefixDtoMechanics.
//
func (ePrefDtoMech errPrefixDtoMechanics) ptr() *errPrefixDtoMechanics {
if ePrefDtoMech.lock == nil {
ePrefDtoMech.lock = new(sync.Mutex)
}
ePrefDtoMech.lock.Lock()
defer ePrefDtoMech.lock.Unlock()
return &errPrefixDtoMechanics{
lock: new(sync.Mutex),
}
}
// get2dEPrefStrings - Receives a pointer to an instance of
// ErrPrefixDto and proceeds to convert the internal collection of
// ErrorPrefixInfo objects to a two dimensional string array
// which is returned to the caller.
//
// The two-dimensional string array contains both error prefix and
// error context information. The Error Prefix string is always in
// the [x][0] position. The Error Context string is always in the
// [x][1] position. The Error Context string is optional and may be
// an empty string.
//
func (ePrefDtoMech *errPrefixDtoMechanics) get2dEPrefStrings(
errPrefDto *ErrPrefixDto,
errorPrefStr string) (
twoDStrArray [][2]string,
err error) {
if ePrefDtoMech.lock == nil {
ePrefDtoMech.lock = new(sync.Mutex)
}
ePrefDtoMech.lock.Lock()
defer ePrefDtoMech.lock.Unlock()
methodNames := errorPrefStr + "\n" +
"errPrefixDtoMechanics.get2dEPrefStrings()"
twoDStrArray = nil
if errPrefDto == nil {
err = fmt.Errorf("%v\n"+
"Error: Input Parameter 'errPrefDto' is invalid!\n"+
"'errPrefDto' is a 'nil' pointer.\n",
methodNames)
return twoDStrArray, err
}
colLen := len(errPrefDto.ePrefCol)
if colLen == 0 {
return twoDStrArray, err
}
twoDStrArray = make([][2]string, colLen)
for i := 0; i < colLen; i++ {
twoDStrArray[i][0] =
errPrefDto.ePrefCol[i].errorPrefixStr
twoDStrArray[i][1] =
errPrefDto.ePrefCol[i].errorContextStr
}
return twoDStrArray, err
}
// setFromIBasicErrorPrefix - Receives an ErrPrefixDto object and
// an empty interface. If that empty interface is convertible to
// one of the 10-valid types listed below, this method then proceeds
// to overwrite and reset the ErrPrefixDto object's ErrorPrefixInfo
// collection containing error prefix and error context
// information. New error information will be extracted from input
// parameter, 'iEPref'. 'iEPref' is an empty interface which must
// be convertible to one of the following valid types:
//
//
// 1. nil - A nil value is valid and generates an empty
// collection of error prefix and error context
// information.
//
// 2. Stringer - The Stringer interface from the 'fmt' package.
// This interface has only one method:
// type Stringer interface {
// String() string
// }
//
// 3. string - A string containing error prefix information.
//
// 4. []string - A one-dimensional slice of strings containing
// error prefix information
//
// 5. [][2]string - A two-dimensional slice of strings
// containing error prefix and error context
// information.
//
// 6. strings.Builder - An instance of strings.Builder. Error prefix
// information will be imported into the new
// returned instance of ErrPrefixDto.
//
// 7 *strings.Builder - A pointer to an instance of strings.Builder.
// Error prefix information will be imported into
// the new returned instance of ErrPrefixDto.
//
// 8. ErrPrefixDto - An instance of ErrPrefixDto. The
// ErrorPrefixInfo from this object will be
// copied to the new returned instance of
// ErrPrefixDto.
//
// 9. *ErrPrefixDto - A pointer to an instance of ErrPrefixDto.
// ErrorPrefixInfo from this object will be
// copied to 'errPrefDto'.
//
// 10. IBasicErrorPrefix - An interface to a method generating
// a two-dimensional slice of strings
// containing error prefix and error
// context information.
//
// Any types not listed above will be considered invalid and
// trigger the return of an error.
//
//
// ----------------------------------------------------------------
//
// Input Parameters
//
// errPrefDto *ErrPrefixDto
// - A pointer to an instance of ErrPrefixDto. Data values in
// this ErrPrefixDto object will be overwritten an set to new
// values extracted from input parameter, 'iEPref'.
//
//
// iEPref interface{}
// - An empty interface containing one of the 7-valid type
// listed above. Any one of these valid types will generate
// valid error prefix and error context information.
//
// If 'iEPref' is NOT convertible to one of the 7-valid types
// listed above, this method will return an error.
//
//
// errorPrefStr string
// - This parameter contains an error prefix string which is
// included in all returned error messages. Usually, it
// contains the names of the calling method or methods.
//
// If no error prefix information is needed, set this parameter
// to an empty string: "".
//
//
// -----------------------------------------------------------------
//
// Return Values
//
// error
// - If this method completes successfully, the returned error
// Type is set equal to 'nil'.
//
// If errors are encountered during processing, the returned
// error Type will encapsulate an error message. This
// returned error message will incorporate the method chain
// and text passed by input parameter, 'errorPrefStr'. The
// 'errorPrefStr' text will be attached to the beginning of
// the error message.
//
func (ePrefDtoMech *errPrefixDtoMechanics) setFromEmptyInterface(
errPrefDto *ErrPrefixDto,
iEPref interface{},
errorPrefStr string) error {
if ePrefDtoMech.lock == nil {
ePrefDtoMech.lock = new(sync.Mutex)
}
ePrefDtoMech.lock.Lock()
defer ePrefDtoMech.lock.Unlock()
methodNames := errorPrefStr + "\n" +
"errPrefixDtoMechanics.setFromEmptyInterface()"
if errPrefDto == nil {
return fmt.Errorf("%v\n"+
"Error: Input Parameter 'errPrefDto' is invalid!\n"+
"'errPrefDto' is a 'nil' pointer.\n",
methodNames)
}
ePrefDtoQuark := errPrefixDtoQuark{}
ePrefDtoQuark.normalizeErrPrefixDto(errPrefDto)
errNanobot := errPrefixDtoNanobot{}
ePrfAtom := errPrefixDtoAtom{}
var backup ErrPrefixDto
var err2 error
originalDataIsGood := true
backup,
err2 = ePrfAtom.copyOutErrPrefDto(
errPrefDto,
"")
if err2 != nil {
originalDataIsGood = false
}
_ = ePrefDtoQuark.
emptyErrPrefInfoCollection(
errPrefDto,
"")
if iEPref == nil {
return nil
}
var ok bool
var str string
// string
str,
ok = iEPref.(string)
if ok {
return errNanobot.setFromString(
errPrefDto,
str,
methodNames)
}
var strs []string
// string array
strs,
ok = iEPref.([]string)
if ok {
return errNanobot.setFromStringArray(
errPrefDto,
strs,
methodNames)
}
var twoDStrArray [][2]string
// Two Dimensional String Array
twoDStrArray,
ok = iEPref.([][2]string)
if ok {
return errNanobot.setFromTwoDStrArray(
errPrefDto,
twoDStrArray,
methodNames)
}
var dto ErrPrefixDto
// ErrPrefixDto
dto,
ok = iEPref.(ErrPrefixDto)
if ok {
return ePrfAtom.
copyInErrPrefDto(
errPrefDto,
&dto,
methodNames)
}
var dtoPtr *ErrPrefixDto
// *ErrPrefixDto
dtoPtr,
ok = iEPref.(*ErrPrefixDto)
if ok {
return ePrfAtom.
copyInErrPrefDto(
errPrefDto,
dtoPtr,
methodNames)
}
var strBuildr strings.Builder
// strings.Builder
strBuildr,
ok = iEPref.(strings.Builder)
if ok {
return errNanobot.setFromStringBuilder(
errPrefDto,
&strBuildr,
methodNames)
}
var strBuildrPtr *strings.Builder
// *strings.Builder
strBuildrPtr,
ok = iEPref.(*strings.Builder)
if ok {
return errNanobot.setFromStringBuilder(
errPrefDto,
strBuildrPtr,
methodNames)
}
var iBasicEPref IBasicErrorPrefix
// IBasicErrorPrefix
iBasicEPref,
ok = iEPref.(IBasicErrorPrefix)
if ok {
return errNanobot.setFromIBasicErrorPrefix(
errPrefDto,
iBasicEPref,
methodNames)
}
var iStr fmt.Stringer
// fmt.Stringer
iStr,
ok = iEPref.(fmt.Stringer)
if ok {
if iStr == nil {
return nil
}
return errNanobot.setFromString(
errPrefDto,
iStr.String(),
methodNames)
}
// An Error Occurred!
// An Error Condition NOW EXISTS!
// Attempt to recover original data
//
var errMsg string
errMsg =
fmt.Sprintf("%v\n"+
"Error: Could NOT extract error prefix information\n"+
"from input parameter 'iEPref'.\n"+
"'iEPref' IS NOT convertible to one of the seven\n"+
"supported types listed as follows:\n"+
" nil\n"+
" fmt.Stringer\n"+
" string\n"+
" []string\n"+
" [][2]string\n"+
" strings.Builder"+
" *strings.Builder"+
" ErrPrefixDto\n"+
" *ErrPrefixDto\n"+
" IBasicErrorPrefix\n",
methodNames)
if originalDataIsGood {
err2 = ePrfAtom.copyInErrPrefDto(
errPrefDto,
&backup,
"")
if err2 != nil {
errMsg +=
fmt.Sprintf("An additional error occurred while attempting to\n"+
"recover original data for the 'errPrefDto' parameter. Therefore,\n"+
"'errPrefDto' now contains corrupted data.\n"+
"2ndError=\n%v\n",
err2.Error())
}
} else {
errMsg +=
"An additional error occurred while attempting to\n" +
"recover original data for the 'errPrefDto' parameter. Therefore,\n" +
"'errPrefDto' now contains corrupted data.\n"
}
return fmt.Errorf("%v",
errMsg)
}