-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
70 lines (54 loc) · 1.74 KB
/
errors.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
package gotables
import (
"errors"
"fmt"
)
/*
Basic gotables error handling:
(1) Define a type struct: <type-name>Error
(2) Define a method Error() string to implement error.
(3) Define a factory function New<type-name>Error(...) *<type-name>Error.
(4) Define a function HasGet<type-name>Error (bool, *<type-name>Error)
(5) Define methods to get <type-name>Error struct private members.
*/
type CircRefError struct {
rootTable *Table
circTable *Table
msg string
}
func (circError *CircRefError) Error() string {
return circError.msg
}
func NewCircRefError(rootTable *Table, circTable *Table, userMsg string) *CircRefError {
var circError CircRefError
circError.rootTable = rootTable
circError.circTable = circTable
if userMsg == "" {
circError.msg = fmt.Sprintf("CircRefError: circular reference in table [%s]: a reference to table [%s] already exists",
circError.rootTable.Name(),
circError.circTable.Name())
} else {
// Use user-defined msg.
circError.msg = "CircRefError: " + userMsg
}
return &circError
}
// Check to see if err has a wrapped CircRefError inside.
func HasCircRefError(err error) (has bool) {
// second argument to errors.As must be a pointer to an interface or a type implementing error
var circError *CircRefError
has = errors.As(err, &circError)
return
}
// Check to see if err has a wrapped CircRefError inside, and get CircRefError if inside.
func GetCircRefError(err error) (circError *CircRefError) {
// second argument to errors.As must be a pointer to an interface or a type implementing error
errors.As(err, &circError)
return
}
func (circError *CircRefError) RootTable() *Table {
return circError.rootTable
}
func (circError *CircRefError) CircTable() *Table {
return circError.circTable
}