forked from jphackworth/kestrel
/
error.go
110 lines (97 loc) · 3.07 KB
/
error.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
package main
const (
ERROR_NONE = 0
ERROR_MALFORMED_ADDRESS = 1
ERROR_FLOOD = 2
ERROR_LINK_LIMIT_EXCEEDED = 3
ERROR_OVERSIZE_MESSAGE = 4
ERROR_UNDERSIZE_MESSAGE = 5
ERROR_AUTHENTICATION = 6
ERROR_INVALID = 7
ERROR_UNDELIVERABLE = 8
ERROR_LOOP_ROUTE = 9
ERROR_RETURN_PATH_INVALID = 10
ERROR_UNKNOWN = 11
ERROR_NOT_IMPLEMENTED = 12
)
var (
cjdnsErrors = map[int]string{
0: "ERROR_NONE",
1: "ERROR_MALFORMED_ADDRESS",
2: "ERROR_FLOOD",
3: "ERROR_LINK_LIMIT_EXCEEDED",
4: "ERROR_OVERSIZE_MESSAGE",
5: "ERROR_UNDERSIZE_MESSAGE",
6: "ERROR_AUTHENTICATION",
7: "ERROR_INVALID",
8: "ERROR_UNDELIVERABLE",
9: "ERROR_LOOP_ROUTE",
10: "ERROR_RETURN_PATH_INVALID",
11: "ERROR_UNKNOWN",
12: "ERROR_NOT_IMPLEMENTED",
}
errNone = newError(0, "No error")
errMalformedAddress = newError(1, "Malformed address")
errFlood = newError(2, "Traffic flood")
errLinkLimitExceeded = newError(3, "Link limit exceeded")
errOverSizeMessage = newError(4, "Oversize message")
errUndersizeMessage = newError(5, "Undersize message")
errAuthentication = newError(6, "Authentication error")
errInvalid = newError(7, "Invalid") // TODO: check what/when raises this type of error
errUndeliverable = newError(8, "Undeliverable")
errLoopRoute = newError(9, "Invalid route due to loop")
errReturnPathInvalid = newError(10, "Invalid return path")
errUnknown = newError(11, "Unknown Error")
errNotImplemented = newError(12, "Feature not implemented")
)
// type protocolError interface {
// error
// Timeout() bool
// Temporary() bool
// }
// type OpError struct {
// Op string // type of operation being performed at time of error
// Addr Addr // peer Address where this occured
// Err error
// }
// Still experimenting with the right approach to handling cjdns-related errors in an
// extensible way. Expect change and placeholders.
type cjdnsError struct {
Code int
Message string
Details string
Timeout bool // is error a timeout?
Temporary bool // is error temporary?
Err error
}
//func newError(errcode int, errmsg string, isTimeoutError bool, isTemporaryError bool) *cjdnsError {
func newError(errcode int, errmsg string) *cjdnsError {
err := &cjdnsError{
Code: errcode,
Message: errmsg,
Details: "",
Timeout: false,
Temporary: false,
}
return err
}
func (err *cjdnsError) Error() string {
return err.Message
}
// addDetails lets you leverage an existing default message type, such as
// errUndeliverable, but allows you to add extra details to the error message
//
// TODO: Check if this is performant, or if there's a better way to do same thing
//
// example: return errUndeliverable.addDetails("decryption failed")
func (err cjdnsError) addDetails(details string) *cjdnsError {
detailedErr := err
detailedErr.Details = details
return &detailedErr
}
func (err *cjdnsError) isTimeout() bool {
return err.Timeout
}
func (err *cjdnsError) isTemporary() bool {
return err.Temporary
}