-
Notifications
You must be signed in to change notification settings - Fork 3
/
error.go
90 lines (75 loc) · 2.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
package socket
import (
"errors"
"net"
"github.com/lthibault/log"
)
var (
// ErrIgnore causes a message to be dropped silently.
// It is typically used when filtering out messgaes that
// originate from the local host.
ErrIgnore = errors.New("ignore")
// ErrClosed is returned when performing operations against
// a closed socket.
ErrClosed = errors.New("closed")
)
// ProtocolError signals a non-fatal error caused either either
// by a malformed *record.Envelope, or by a *Record containing
// unexpected values.
//
// The default error callback will log a protocol error at the
// DEBUG level, using 'Message' as the logging message and the
// 'Meta' field as a set of structured logging fields. If the
// 'Cause' field is non-nil, it will be added to 'Meta' before
// writing the log message.
//
// User-supplied error handlers SHOULD test for ProtocolError
// via type-assertion and treat any instances as a non-fatal
// error.
type ProtocolError struct {
Message string
Cause error
Meta log.F
}
func (pe ProtocolError) Loggable() map[string]interface{} {
if pe.Cause != nil {
pe.Meta["error"] = pe.Cause
}
return pe.Meta
}
func (pe ProtocolError) Error() string {
return pe.Cause.Error()
}
func (pe ProtocolError) Is(err error) bool {
return errors.Is(err, pe.Cause)
}
func (pe ProtocolError) Unwrap() error {
return pe.Cause
}
// ValidationError signals that a packet contains the expected
// data, but that its authenticity and provenance could not be
// proven.
//
// The default error callback will log validation errors at the
// DEBUG level. In high-security environments, it MAY be
// adviseable to log such events at the WARN level, and to take
// further action.
type ValidationError struct {
Cause error
From net.Addr
}
func (ve ValidationError) Loggable() map[string]interface{} {
return log.F{
"error": ve.Cause,
"from": ve.From,
}
}
func (ve ValidationError) Error() string {
return ve.Cause.Error()
}
func (ve ValidationError) Is(err error) bool {
return errors.Is(err, ve.Cause)
}
func (ve ValidationError) Unwrap() error {
return ve.Cause
}