Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit 3f77a13

Browse files
authored
Make errors an enum type (#116)
1 parent 7cd664d commit 3f77a13

File tree

4 files changed

+31
-24
lines changed

4 files changed

+31
-24
lines changed

outline/connectivity/connectivity.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type reachabilityError struct {
4848
// the current network. Parallelizes the execution of TCP and UDP checks, selects the appropriate
4949
// error code to return accounting for transient network failures.
5050
// Returns an error if an unexpected error ocurrs.
51-
func CheckConnectivity(client *outline.Client) (int, error) {
51+
func CheckConnectivity(client *outline.Client) (neterrors.Error, error) {
5252
// Start asynchronous UDP support check.
5353
udpChan := make(chan error)
5454
go func() {

outline/electron/main.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ func main() {
8888
// Validate proxy flags
8989
if *args.proxyHost == "" {
9090
log.Errorf("Must provide a Shadowsocks proxy host name or IP address")
91-
os.Exit(neterrors.IllegalConfiguration)
91+
os.Exit(neterrors.IllegalConfiguration.Number())
9292
} else if *args.proxyPort <= 0 || *args.proxyPort > 65535 {
9393
log.Errorf("Must provide a valid Shadowsocks proxy port [1:65535]")
94-
os.Exit(neterrors.IllegalConfiguration)
94+
os.Exit(neterrors.IllegalConfiguration.Number())
9595
} else if *args.proxyPassword == "" {
9696
log.Errorf("Must provide a Shadowsocks proxy password")
97-
os.Exit(neterrors.IllegalConfiguration)
97+
os.Exit(neterrors.IllegalConfiguration.Number())
9898
} else if *args.proxyCipher == "" {
9999
log.Errorf("Must provide a Shadowsocks proxy encryption cipher")
100-
os.Exit(neterrors.IllegalConfiguration)
100+
os.Exit(neterrors.IllegalConfiguration.Number())
101101
}
102102

103103
config := shadowsocks.Config{
@@ -114,15 +114,15 @@ func main() {
114114
for i, r := range prefixRunes {
115115
if (r & 0xFF) != r {
116116
log.Errorf("Character out of range: %r", r)
117-
os.Exit(neterrors.IllegalConfiguration)
117+
os.Exit(neterrors.IllegalConfiguration.Number())
118118
}
119119
config.Prefix[i] = byte(r)
120120
}
121121

122122
client, err := shadowsocks.NewClient(&config)
123123
if err != nil {
124124
log.Errorf("Failed to construct Shadowsocks client: %v", err)
125-
os.Exit(neterrors.IllegalConfiguration)
125+
os.Exit(neterrors.IllegalConfiguration.Number())
126126
}
127127

128128
if *args.checkConnectivity {
@@ -131,15 +131,15 @@ func main() {
131131
if err != nil {
132132
log.Errorf("Failed to perform connectivity checks: %v", err)
133133
}
134-
os.Exit(connErrCode)
134+
os.Exit(connErrCode.Number())
135135
}
136136

137137
// Open TUN device
138138
dnsResolvers := strings.Split(*args.tunDNS, ",")
139139
tunDevice, err := tun.OpenTunDevice(*args.tunName, *args.tunAddr, *args.tunGw, *args.tunMask, dnsResolvers, persistTun)
140140
if err != nil {
141141
log.Errorf("Failed to open TUN device: %v", err)
142-
os.Exit(neterrors.SystemMisconfigured)
142+
os.Exit(neterrors.SystemMisconfigured.Number())
143143
}
144144
// Output packets to TUN device
145145
core.RegisterOutputFn(tunDevice.Write)
@@ -160,7 +160,7 @@ func main() {
160160
_, err := io.CopyBuffer(lwipWriter, tunDevice, make([]byte, mtu))
161161
if err != nil {
162162
log.Errorf("Failed to write data to network stack: %v", err)
163-
os.Exit(neterrors.Unexpected)
163+
os.Exit(neterrors.Unexpected.Number())
164164
}
165165
}()
166166

outline/neterrors/neterrors.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,25 @@
1818
// provide its own standalone API, leaving translations to the consumer.
1919
package neterrors
2020

21+
type Error int
22+
23+
func (e Error) Number() int {
24+
return int(e)
25+
}
26+
2127
// Outline error codes. Must be kept in sync with definitions in https://github.com/Jigsaw-Code/outline-client/blob/master/src/www/model/errors.ts
2228
const (
23-
NoError = 0
24-
Unexpected = 1
25-
NoVPNPermissions = 2 // Unused
26-
AuthenticationFailure = 3
27-
UDPConnectivity = 4
28-
Unreachable = 5
29-
VpnStartFailure = 6 // Unused
30-
IllegalConfiguration = 7 // Electron only
31-
ShadowsocksStartFailure = 8 // Unused
32-
ConfigureSystemProxyFailure = 9 // Unused
33-
NoAdminPermissions = 10 // Unused
34-
UnsupportedRoutingTable = 11 // Unused
35-
SystemMisconfigured = 12 // Electron only
29+
NoError Error = 0
30+
Unexpected Error = 1
31+
NoVPNPermissions Error = 2 // Unused
32+
AuthenticationFailure Error = 3
33+
UDPConnectivity Error = 4
34+
Unreachable Error = 5
35+
VpnStartFailure Error = 6 // Unused
36+
IllegalConfiguration Error = 7 // Electron only
37+
ShadowsocksStartFailure Error = 8 // Unused
38+
ConfigureSystemProxyFailure Error = 9 // Unused
39+
NoAdminPermissions Error = 10 // Unused
40+
UnsupportedRoutingTable Error = 11 // Unused
41+
SystemMisconfigured Error = 12 // Electron only
3642
)

outline/shadowsocks/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/Jigsaw-Code/outline-go-tun2socks/outline"
2424
"github.com/Jigsaw-Code/outline-go-tun2socks/outline/connectivity"
25+
"github.com/Jigsaw-Code/outline-go-tun2socks/outline/neterrors"
2526
"github.com/Jigsaw-Code/outline-internal-sdk/transport"
2627
"github.com/Jigsaw-Code/outline-internal-sdk/transport/shadowsocks"
2728
"github.com/Jigsaw-Code/outline-internal-sdk/transport/shadowsocks/client"
@@ -78,7 +79,7 @@ const reachabilityTimeout = 10 * time.Second
7879
// the current network. Parallelizes the execution of TCP and UDP checks, selects the appropriate
7980
// error code to return accounting for transient network failures.
8081
// Returns an error if an unexpected error ocurrs.
81-
func CheckConnectivity(client *Client) (int, error) {
82+
func CheckConnectivity(client *Client) (neterrors.Error, error) {
8283
return connectivity.CheckConnectivity(client)
8384
}
8485

0 commit comments

Comments
 (0)