Skip to content

Commit 1a79c9d

Browse files
committed
update error struct
1 parent a40c895 commit 1a79c9d

File tree

4 files changed

+27
-44
lines changed

4 files changed

+27
-44
lines changed

Sources/TCP/Socket/TCPClient.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@ extension TCPSocket {
6868
identifier: "getAddressInfo",
6969
possibleCauses: [
7070
"The address supplied could not be resolved."
71-
]
71+
],
72+
source: .capture()
7273
)
7374
}
7475
defer {
7576
freeaddrinfo(result)
7677
}
7778

7879
guard let info = result else {
79-
throw TCPError(identifier: "unwrapAddress", reason: "Could not unwrap address info.")
80+
throw TCPError(identifier: "unwrapAddress", reason: "Could not unwrap address info.", source: .capture())
8081
}
8182

8283
res = cConnect(descriptor, info.pointee.ai_addr, info.pointee.ai_addrlen)
@@ -91,7 +92,7 @@ extension TCPSocket {
9192
ERROR("EINPROGRESS on a blocking socket")
9293
return
9394
}
94-
default: throw TCPError.posix(errno, identifier: "connect")
95+
default: throw TCPError.posix(errno, identifier: "connect", source: .capture())
9596
}
9697
}
9798

Sources/TCP/Socket/TCPServer.swift

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Async
22
import Bits
3+
import Debugging
34
import Dispatch
45
import COperatingSystem
56

@@ -106,20 +107,21 @@ extension TCPSocket {
106107
],
107108
suggestedFixes: [
108109
"Bind to `0.0.0.0` or to your machine's IP address"
109-
]
110+
],
111+
source: .capture()
110112
)
111113
}
112114
defer {
113115
freeaddrinfo(result)
114116
}
115117

116118
guard let info = result else {
117-
throw TCPError(identifier: "unwrapAddress", reason: "Could not unwrap address info.")
119+
throw TCPError(identifier: "unwrapAddress", reason: "Could not unwrap address info.", source: .capture())
118120
}
119121

120122
res = COperatingSystem.bind(descriptor, info.pointee.ai_addr, info.pointee.ai_addrlen)
121123
guard res == 0 else {
122-
throw TCPError.posix(errno, identifier: "bind")
124+
throw TCPError.posix(errno, identifier: "bind", source: .capture())
123125
}
124126
}
125127

@@ -128,7 +130,7 @@ extension TCPSocket {
128130
fileprivate func listen(backlog: Int32 = 4096) throws {
129131
let res = COperatingSystem.listen(descriptor, backlog)
130132
guard res == 0 else {
131-
throw TCPError.posix(errno, identifier: "listen")
133+
throw TCPError.posix(errno, identifier: "listen", source: .capture())
132134
}
133135
}
134136

@@ -143,7 +145,7 @@ extension TCPSocket {
143145
guard descriptor > 0 else {
144146
switch errno {
145147
case EAGAIN: return nil // FIXME: enum return
146-
default: throw TCPError.posix(errno, identifier: "accept")
148+
default: throw TCPError.posix(errno, identifier: "accept", source: .capture())
147149
}
148150
}
149151

@@ -171,18 +173,16 @@ extension TCPError {
171173
identifier: String,
172174
possibleCauses: [String] = [],
173175
suggestedFixes: [String] = [],
174-
file: String = #file,
175-
function: String = #function,
176-
line: UInt = #line,
177-
column: UInt = #column
176+
source: SourceLocation
178177
) -> TCPError {
179178
guard gaires != EAI_SYSTEM else {
180179
return .posix(
181180
errno,
182181
identifier: identifier,
183182
possibleCauses: possibleCauses,
184183
suggestedFixes: suggestedFixes,
185-
file: file, function: function, line: line, column: column)
184+
source: source
185+
)
186186
}
187187
let message = COperatingSystem.gai_strerror(gaires)
188188
let string = String(cString: message!, encoding: .utf8) ?? "unknown"
@@ -191,10 +191,7 @@ extension TCPError {
191191
reason: string,
192192
possibleCauses: possibleCauses,
193193
suggestedFixes: suggestedFixes,
194-
file: file,
195-
function: function,
196-
line: line,
197-
column: column
194+
source: source
198195
)
199196
}
200197
}

Sources/TCP/Socket/TCPSocket.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ public final class TCPSocket {
4242
) throws {
4343
let sockfd = socket(AF_INET, SOCK_STREAM, 0)
4444
guard sockfd > 0 else {
45-
throw TCPError.posix(errno, identifier: "socketCreate")
45+
throw TCPError.posix(errno, identifier: "socketCreate", source: .capture())
4646
}
4747

4848
if isNonBlocking {
4949
// Set the socket to async/non blocking I/O
5050
guard fcntl(sockfd, F_SETFL, O_NONBLOCK) == 0 else {
5151
_ = COperatingSystem.close(sockfd)
52-
throw TCPError.posix(errno, identifier: "setNonBlocking")
52+
throw TCPError.posix(errno, identifier: "setNonBlocking", source: .capture())
5353
}
5454
}
5555

@@ -62,7 +62,7 @@ public final class TCPSocket {
6262
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, intSize) == 0
6363
else {
6464
_ = COperatingSystem.close(sockfd)
65-
throw TCPError.posix(errno, identifier: "setReuseAddress")
65+
throw TCPError.posix(errno, identifier: "setReuseAddress", source: .capture())
6666
}
6767
}
6868

@@ -109,9 +109,9 @@ public final class TCPSocket {
109109
return .wouldBlock
110110
case EBADF:
111111
assert(isClosed, "EBADF when socket not closed")
112-
throw TCPError(identifier: "read", reason: "Socket is closed.")
112+
throw TCPError(identifier: "read", reason: "Socket is closed.", source: .capture())
113113
default:
114-
throw TCPError.posix(errno, identifier: "read")
114+
throw TCPError.posix(errno, identifier: "read", source: .capture())
115115
}
116116
}
117117

@@ -145,11 +145,11 @@ public final class TCPSocket {
145145
return .success(count: 0)
146146
case EBADF:
147147
assert(isClosed, "EBADF when socket not closed")
148-
throw TCPError(identifier: "write", reason: "Socket is closed.")
148+
throw TCPError(identifier: "write", reason: "Socket is closed.", source: .capture())
149149
case EAGAIN, EWOULDBLOCK:
150150
return .wouldBlock
151151
default:
152-
throw TCPError.posix(errno, identifier: "write")
152+
throw TCPError.posix(errno, identifier: "write", source: .capture())
153153
}
154154
}
155155

Sources/TCP/Utilties/TCPError.swift

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ public struct TCPError: Debuggable {
66
public static let readableName = "TCP Error"
77
public let identifier: String
88
public var reason: String
9-
public var file: String
10-
public var function: String
11-
public var line: UInt
12-
public var column: UInt
9+
public var sourceLocation: SourceLocation?
1310
public var stackTrace: [String]
1411
public var possibleCauses: [String]
1512
public var suggestedFixes: [String]
@@ -20,17 +17,11 @@ public struct TCPError: Debuggable {
2017
reason: String,
2118
possibleCauses: [String] = [],
2219
suggestedFixes: [String] = [],
23-
file: String = #file,
24-
function: String = #function,
25-
line: UInt = #line,
26-
column: UInt = #column
20+
source: SourceLocation
2721
) {
2822
self.identifier = identifier
2923
self.reason = reason
30-
self.file = file
31-
self.function = function
32-
self.line = line
33-
self.column = column
24+
self.sourceLocation = source
3425
self.stackTrace = TCPError.makeStackTrace()
3526
self.possibleCauses = possibleCauses
3627
self.suggestedFixes = suggestedFixes
@@ -42,10 +33,7 @@ public struct TCPError: Debuggable {
4233
identifier: String,
4334
possibleCauses: [String] = [],
4435
suggestedFixes: [String] = [],
45-
file: String = #file,
46-
function: String = #function,
47-
line: UInt = #line,
48-
column: UInt = #column
36+
source: SourceLocation
4937
) -> TCPError {
5038
let message = COperatingSystem.strerror(errno)
5139
let string = String(cString: message!, encoding: .utf8) ?? "unknown"
@@ -54,10 +42,7 @@ public struct TCPError: Debuggable {
5442
reason: string,
5543
possibleCauses: possibleCauses,
5644
suggestedFixes: suggestedFixes,
57-
file: file,
58-
function: function,
59-
line: line,
60-
column: column
45+
source: source
6146
)
6247
}
6348
}

0 commit comments

Comments
 (0)