-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
129 lines (109 loc) · 2.62 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
123
124
125
126
127
128
129
package socksauth
import (
"errors"
"fmt"
"runtime"
"strings"
)
type SocksError interface {
error
Is(target error) bool
Unwrap() error
ProxyServer() struct {
Host string
Name string
}
DestinationServer() string
ConnectionId() int64
withError(err error) SocksError
withMessage(message string) SocksError
fromConnection(socksConnection) SocksError
}
var _ SocksError = (*socksError)(nil)
type socksError struct {
code string
err error
Message string
ErrOrigin string
destinationServer string
proxyServer struct {
Name string
Host string
}
connectionId int64
}
func newError(code, defaultMessage string) socksError {
_, file, line, _ := runtime.Caller(1)
return socksError{
code: code,
Message: defaultMessage,
ErrOrigin: fmt.Sprintf("%s:%d", file, line),
}
}
func (e socksError) Error() string {
connInfos := make([]string, 0)
if e.connectionId != 0 {
connInfos = append(connInfos, fmt.Sprintf("connection %d", e.connectionId))
}
if e.proxyServer.Name != "" && e.proxyServer.Host != "" {
connInfos = append(connInfos, fmt.Sprintf("proxy %s (%s)", e.proxyServer.Name, e.proxyServer.Host))
} else if e.proxyServer.Host != "" {
connInfos = append(connInfos, fmt.Sprintf("proxy %s", e.proxyServer.Host))
}
if e.destinationServer != "" {
connInfos = append(connInfos, fmt.Sprintf("destination %s", e.destinationServer))
}
asString := fmt.Sprintf("%s - [%s] %s", e.code, strings.Join(connInfos, " | "), e.Message)
if e.err != nil {
asString += ": " + e.err.Error()
}
if e.ErrOrigin != "" {
asString += " at " + e.ErrOrigin
}
return asString
}
func (e socksError) Is(target error) bool {
t, ok := target.(socksError)
if !ok {
return false
}
return e.code == t.code
}
func (e socksError) Unwrap() error {
return e.err
}
func (e socksError) ProxyServer() struct {
Host string
Name string
} {
return struct {
Host string
Name string
}{Host: e.proxyServer.Host, Name: e.proxyServer.Name}
}
func (e socksError) DestinationServer() string {
return e.destinationServer
}
func (e socksError) ConnectionId() int64 {
return e.connectionId
}
func (e socksError) withError(err error) SocksError {
e.err = errors.Join(e.err, err)
return e
}
func (e socksError) withMessage(message string) SocksError {
e.Message = message
return e
}
func (e socksError) fromConnection(conn socksConnection) SocksError {
e.connectionId = conn.connId
e.proxyServer.Name = conn.proxyName
e.proxyServer.Host = conn.proxyHost
e.destinationServer = conn.destination
_, file, line, ok := runtime.Caller(1)
if !ok {
return e
}
e.ErrOrigin = fmt.Sprintf("%s:%d", file, line)
return e
}