-
Notifications
You must be signed in to change notification settings - Fork 3
/
error.go
122 lines (100 loc) · 3.74 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
111
112
113
114
115
116
117
118
119
120
121
122
package p2p
import (
"errors"
"fmt"
"time"
)
var (
// ErrPeerNotFound should be returned by p2p service methods when the requested
// peer is not found.
ErrPeerNotFound = errors.New("peer not found")
// ErrAlreadyConnected is returned if connect was called for already connected node.
ErrAlreadyConnected = errors.New("already connected")
// ErrDialLightNode is returned if connect was attempted to a light node.
ErrDialLightNode = errors.New("target peer is a light node")
// ErrPeerBlocklisted is returned if peer is on blocklist
ErrPeerBlocklisted = errors.New("peer blocklisted")
ErrStreamClosed = errors.New("stream closed")
ErrNetworkUnavailable = errors.New("network unavailable")
)
const (
DefaultBlocklistTime = 1 * time.Minute
)
// ConnectionBackoffError indicates that connection calls will not be executed until `tryAfter` timetamp.
// The reason is provided in the wrappped error.
type ConnectionBackoffError struct {
tryAfter time.Time
err error
}
// NewConnectionBackoffError creates new `ConnectionBackoffError` with provided underlying error and `tryAfter` timestamp.
func NewConnectionBackoffError(err error, tryAfter time.Time) error {
return &ConnectionBackoffError{err: err, tryAfter: tryAfter}
}
// TryAfter returns a tryAfter timetamp.
func (e *ConnectionBackoffError) TryAfter() time.Time {
return e.tryAfter
}
// Unwrap returns an underlying error.
func (e *ConnectionBackoffError) Unwrap() error { return e.err }
// Error implements function of the standard go error interface.
func (e *ConnectionBackoffError) Error() string {
return e.err.Error()
}
// DisconnectError is an error that is specifically handled inside p2p. If returned by specific protocol
// handler it causes peer disconnect.
type DisconnectError struct {
err error
}
// NewDisconnectError wraps error and creates a special error that is treated specially
// by p2p. It causes peer to disconnect.
func NewDisconnectError(err error) error {
return &DisconnectError{
err: err,
}
}
// Unwrap returns an underlying error.
func (e *DisconnectError) Unwrap() error { return e.err }
// Error implements function of the standard go error interface.
func (e *DisconnectError) Error() string {
return e.err.Error()
}
type BlockPeerError struct {
duration time.Duration
err error
}
// NewBlockPeerError wraps error and creates a special error that is treated specially
// by p2p. It causes peer to be disconnected and blocks any new connection for this peer for the provided duration.
func NewBlockPeerError(duration time.Duration, err error) error {
return &BlockPeerError{
duration: duration,
err: err,
}
}
// Unwrap returns an underlying error.
func (e *BlockPeerError) Unwrap() error { return e.err }
// Error implements function of the standard go error interface.
func (e *BlockPeerError) Error() string {
return e.err.Error()
}
// Duration represents the period for which the peer will be blocked.
// 0 duration is treated as infinity
func (e *BlockPeerError) Duration() time.Duration {
return e.duration
}
// IncompatibleStreamError is the error that should be returned by p2p service
// NewStream method when the stream or its version is not supported.
type IncompatibleStreamError struct {
err error
}
// NewIncompatibleStreamError wraps the error that is the cause of stream
// incompatibility with IncompatibleStreamError that it can be detected and
// returns it.
func NewIncompatibleStreamError(err error) *IncompatibleStreamError {
return &IncompatibleStreamError{err: err}
}
// Unwrap returns an underlying error.
func (e *IncompatibleStreamError) Unwrap() error { return e.err }
// Error implements function of the standard go error interface.
func (e *IncompatibleStreamError) Error() string {
return fmt.Sprintf("incompatible stream: %v", e.err)
}