-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
113 lines (96 loc) · 3.19 KB
/
errors.go
File metadata and controls
113 lines (96 loc) · 3.19 KB
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
package netgo
import (
"errors"
"fmt"
"net"
)
// Sentinel errors for netgo operations.
// These errors can be checked using errors.Is() for proper error handling.
var (
// ErrRecvTimeout indicates that a receive operation timed out.
ErrRecvTimeout = errors.New("recv timeout")
// ErrPushToSendQueueTimeout indicates that pushing to the send queue timed out.
ErrPushToSendQueueTimeout = errors.New("push to send queue timeout")
// ErrSendQueueFull indicates that the send queue is full.
ErrSendQueueFull = errors.New("send queue full")
// ErrAsynSendTimeout indicates that an asynchronous send operation timed out.
ErrAsynSendTimeout = errors.New("async send timeout")
// ErrSocketClosed indicates that the socket has been closed.
ErrSocketClosed = errors.New("socket closed")
// ErrShutdown indicates that shutdown is in progress.
ErrShutdown = errors.New("shutdown in progress")
// ErrInvalidPacket indicates that a packet is invalid (e.g., zero payload).
ErrInvalidPacket = errors.New("invalid packet")
// ErrPacketTooLarge indicates that a packet exceeds the maximum allowed size.
ErrPacketTooLarge = errors.New("packet too large")
)
// IsTimeout checks if the error is a timeout error.
// This includes both netgo timeout errors and underlying network timeouts.
func IsTimeout(err error) bool {
if err == nil {
return false
}
if errors.Is(err, ErrRecvTimeout) ||
errors.Is(err, ErrAsynSendTimeout) ||
errors.Is(err, ErrPushToSendQueueTimeout) {
return true
}
return IsNetTimeoutError(err)
}
// IsNetTimeoutError checks if the error is a net.Error with Timeout() true.
func IsNetTimeoutError(err error) bool {
if e, ok := err.(net.Error); ok && e.Timeout() {
return true
}
return false
}
// IsClosed checks if the error indicates the socket/connection is closed.
func IsClosed(err error) bool {
if err == nil {
return false
}
if errors.Is(err, ErrSocketClosed) || errors.Is(err, ErrShutdown) {
return true
}
// Check for common closed connection errors
return isClosedError(err)
}
// isClosedError checks for common closed connection error patterns.
func isClosedError(err error) bool {
if err == nil {
return false
}
errStr := err.Error()
// Common patterns indicating closed connections
return containsString(errStr, "use of closed network connection") ||
containsString(errStr, "broken pipe") ||
containsString(errStr, "connection reset") ||
containsString(errStr, "EOF")
}
// WrapError wraps an error with additional context.
func WrapError(err error, message string) error {
if err == nil {
return nil
}
return fmt.Errorf("%s: %w", message, err)
}
// WrapErrorf wraps an error with formatted context.
func WrapErrorf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), err)
}
// containsString checks if s contains substr (case-sensitive).
func containsString(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
(len(s) > 0 && len(substr) > 0 && findSubstring(s, substr)))
}
func findSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}