Skip to content

Commit 64c90c4

Browse files
committed
Updated POSIXError
1 parent 126fa2c commit 64c90c4

File tree

8 files changed

+98
-48
lines changed

8 files changed

+98
-48
lines changed

Sources/BluetoothLinux/DeviceCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ internal func HCISendCommand <T: HCICommand> (_ deviceDescriptor: CInt,
4747

4848
// write to device descriptor socket
4949
guard write(deviceDescriptor, &data, data.count) >= 0 // should we check if all data was written?
50-
else { throw POSIXError.fromErrno! }
50+
else { throw POSIXError.fromErrno() }
5151
}

Sources/BluetoothLinux/DevicePollEvent.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal func HCIPollEvent(_ deviceDescriptor: CInt,
4444
guard withUnsafeMutablePointer(to: &oldFilter, {
4545
let pointer = UnsafeMutableRawPointer($0)
4646
return getsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, pointer, &oldFilterLength) == 0
47-
}) else { throw POSIXError.fromErrno! }
47+
}) else { throw POSIXError.fromErrno() }
4848

4949
var newFilter = HCIFilter()
5050
newFilter.clear()
@@ -56,15 +56,15 @@ internal func HCIPollEvent(_ deviceDescriptor: CInt,
5656
guard withUnsafeMutablePointer(to: &newFilter, {
5757
let pointer = UnsafeMutableRawPointer($0)
5858
return setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, pointer, newFilterLength) == 0
59-
}) else { throw POSIXError.fromErrno! }
59+
}) else { throw POSIXError.fromErrno() }
6060

6161
// restore old filter in case of error
6262
func restoreFilter(_ error: Error) -> Error {
6363

6464
guard withUnsafeMutablePointer(to: &oldFilter, {
6565
let pointer = UnsafeMutableRawPointer($0)
6666
return setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, pointer, newFilterLength) == 0
67-
}) else { return BluetoothHostControllerError.couldNotRestoreFilter(error, POSIXError.fromErrno!) }
67+
}) else { return BluetoothHostControllerError.couldNotRestoreFilter(error, POSIXError.fromErrno()) }
6868

6969
return error
7070
}
@@ -102,7 +102,7 @@ internal func HCIPollEvent(_ deviceDescriptor: CInt,
102102
} else {
103103

104104
// attempt to restore filter and throw
105-
throw restoreFilter(POSIXError.fromErrno!)
105+
throw restoreFilter(POSIXError.fromErrno())
106106
}
107107
}
108108

Sources/BluetoothLinux/DeviceRequest.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ internal func HCISendRequest <Command: HCICommand> (_ deviceDescriptor: CInt,
208208

209209
// get old filter
210210
guard getsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, oldFilterPointer, &filterLength) == 0
211-
else { throw POSIXError.fromErrno! }
211+
else { throw POSIXError.fromErrno() }
212212

213213
// configure new filter
214214
newFilter.clear()
@@ -222,13 +222,13 @@ internal func HCISendRequest <Command: HCICommand> (_ deviceDescriptor: CInt,
222222

223223
// set new filter
224224
guard setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, newFilterPointer, filterLength) == 0
225-
else { throw POSIXError.fromErrno! }
225+
else { throw POSIXError.fromErrno() }
226226

227227
// restore old filter in case of error
228228
func restoreFilter(_ error: Error) -> Error {
229229

230230
guard setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, oldFilterPointer, filterLength) == 0
231-
else { return BluetoothHostControllerError.couldNotRestoreFilter(error, POSIXError.fromErrno!) }
231+
else { return BluetoothHostControllerError.couldNotRestoreFilter(error, POSIXError.fromErrno()) }
232232

233233
return error
234234
}
@@ -268,7 +268,7 @@ internal func HCISendRequest <Command: HCICommand> (_ deviceDescriptor: CInt,
268268
} else {
269269

270270
// attempt to restore filter and throw
271-
throw restoreFilter(POSIXError.fromErrno!)
271+
throw restoreFilter(POSIXError.fromErrno())
272272
}
273273
}
274274

@@ -298,7 +298,7 @@ internal func HCISendRequest <Command: HCICommand> (_ deviceDescriptor: CInt,
298298
} else {
299299

300300
// attempt to restore filter and throw
301-
throw restoreFilter(POSIXError.fromErrno!)
301+
throw restoreFilter(POSIXError.fromErrno())
302302
}
303303
}
304304

@@ -317,7 +317,7 @@ internal func HCISendRequest <Command: HCICommand> (_ deviceDescriptor: CInt,
317317
func done() throws {
318318

319319
guard setsockopt(deviceDescriptor, SOL_HCI, HCISocketOption.Filter.rawValue, oldFilterPointer, filterLength) == 0
320-
else { throw POSIXError.fromErrno! }
320+
else { throw POSIXError.fromErrno() }
321321
}
322322

323323
switch eventHeader.event {

Sources/BluetoothLinux/HostController.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ internal func HCIOpenDevice(_ deviceIdentifier: UInt16) throws -> CInt {
121121
// Create HCI socket
122122
let hciSocket = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothProtocol.hci.rawValue)
123123

124-
guard hciSocket >= 0 else { throw POSIXError.fromErrno! }
124+
guard hciSocket >= 0 else { throw POSIXError.fromErrno() }
125125

126126
// Bind socket to the HCI device
127127
var address = HCISocketAddress()
@@ -136,7 +136,7 @@ internal func HCIOpenDevice(_ deviceIdentifier: UInt16) throws -> CInt {
136136

137137
guard didBind else {
138138
close(hciSocket)
139-
throw POSIXError.fromErrno!
139+
throw POSIXError.fromErrno()
140140
}
141141

142142
return hciSocket
@@ -147,7 +147,7 @@ internal func HCIRequestDeviceList <T> (_ response: (_ hciSocket: CInt, _ list:
147147
// open HCI socket
148148
let hciSocket = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothProtocol.hci.rawValue)
149149

150-
guard hciSocket >= 0 else { throw POSIXError.fromErrno! }
150+
guard hciSocket >= 0 else { throw POSIXError.fromErrno() }
151151

152152
defer { close(hciSocket) }
153153

@@ -160,7 +160,7 @@ internal func HCIRequestDeviceList <T> (_ response: (_ hciSocket: CInt, _ list:
160160
IOControl(hciSocket, HCI.IOCTL.GetDeviceList, $0)
161161
}
162162

163-
guard ioctlValue >= 0 else { throw POSIXError.fromErrno! }
163+
guard ioctlValue >= 0 else { throw POSIXError.fromErrno() }
164164

165165
return try response(hciSocket, &deviceList)
166166
}
@@ -216,7 +216,7 @@ internal func HCIGetRoute(_ address: BluetoothAddress? = nil) throws -> UInt16?
216216

217217
guard withUnsafeMutablePointer(to: &deviceInfo, {
218218
IOControl(CInt(dd), HCI.IOCTL.GetDeviceInfo, UnsafeMutableRawPointer($0)) }) == 0
219-
else { throw POSIXError.fromErrno! }
219+
else { throw POSIXError.fromErrno() }
220220

221221
return deviceInfo.address == address
222222
}
@@ -228,7 +228,7 @@ internal func HCIDeviceInfo(_ deviceIdentifier: UInt16) throws -> HCIDeviceInfor
228228

229229
let hciSocket = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BluetoothProtocol.hci.rawValue)
230230

231-
guard hciSocket >= 0 else { throw POSIXError.fromErrno! }
231+
guard hciSocket >= 0 else { throw POSIXError.fromErrno() }
232232

233233
defer { close(hciSocket) }
234234

@@ -237,7 +237,7 @@ internal func HCIDeviceInfo(_ deviceIdentifier: UInt16) throws -> HCIDeviceInfor
237237

238238
guard withUnsafeMutablePointer(to: &deviceInfo, {
239239
IOControl(hciSocket, HCI.IOCTL.GetDeviceInfo, UnsafeMutableRawPointer($0)) }) == 0
240-
else { throw POSIXError.fromErrno! }
240+
else { throw POSIXError.fromErrno() }
241241

242242
return deviceInfo
243243
}

Sources/BluetoothLinux/L2CAP.swift

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
114114
var optionLength = socklen_t(MemoryLayout<CInt>.size)
115115

116116
guard getsockopt(fileDescriptor, SOL_SOCKET, socketOption, &optionValue, &optionLength) == 0
117-
else { throw POSIXError.fromErrno! }
117+
else { throw POSIXError.fromErrno() }
118118

119119
return optionValue
120120
}
@@ -141,7 +141,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
141141

142142
// error creating socket
143143
guard internalSocket >= 0
144-
else { throw POSIXError.fromErrno! }
144+
else { throw POSIXError.fromErrno() }
145145

146146
// set source address
147147
var localAddress = sockaddr_l2()
@@ -157,7 +157,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
157157
$0.withMemoryRebound(to: sockaddr.self, capacity: 1, {
158158
bind(internalSocket, $0, socklen_t(MemoryLayout<sockaddr_l2>.size)) == 0
159159
})
160-
}) else { close(internalSocket); throw POSIXError.fromErrno! }
160+
}) else { close(internalSocket); throw POSIXError.fromErrno() }
161161

162162
return (internalSocket, localAddress)
163163
}
@@ -200,7 +200,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
200200
security.level = securityLevel.rawValue
201201

202202
guard setsockopt(internalSocket, SOL_BLUETOOTH, BT_SECURITY, &security, socklen_t(MemoryLayout<bt_security>.size)) == 0
203-
else { throw POSIXError.fromErrno! }
203+
else { throw POSIXError.fromErrno() }
204204

205205
self.securityLevel = securityLevel
206206
}
@@ -210,7 +210,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
210210

211211
// put socket into listening mode
212212
guard listen(internalSocket, Int32(queueLimit)) == 0
213-
else { throw POSIXError.fromErrno! }
213+
else { throw POSIXError.fromErrno() }
214214
}
215215

216216
/// Blocks the caller until a new connection is recieved.
@@ -228,7 +228,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
228228
})
229229

230230
// error accepting new connection
231-
guard client >= 0 else { throw POSIXError.fromErrno! }
231+
guard client >= 0 else { throw POSIXError.fromErrno() }
232232

233233
let newSocket = L2CAPSocket(clientSocket: client,
234234
remoteAddress: remoteAddress,
@@ -257,7 +257,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
257257
$0.withMemoryRebound(to: sockaddr.self, capacity: 1, {
258258
connect(internalSocket, $0, socklen_t(MemoryLayout<sockaddr_l2>.size)) == 0
259259
})
260-
}) else { throw POSIXError.fromErrno! }
260+
}) else { throw POSIXError.fromErrno() }
261261

262262
// make socket non-blocking
263263
try setNonblocking()
@@ -276,13 +276,9 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
276276
let actualByteCount = read(internalSocket, &buffer, bufferSize)
277277

278278
guard actualByteCount >= 0 else {
279-
if let error = POSIXError.fromErrno {
280-
throw error
281-
} else {
282-
return nil
283-
}
279+
throw POSIXError.fromErrno()
284280
}
285-
281+
286282
let actualBytes = Array(buffer.prefix(actualByteCount))
287283

288284
return Data(actualBytes)
@@ -299,7 +295,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
299295
let fdCount = select(internalSocket + 1, &readSockets, nil, nil, &time)
300296

301297
guard fdCount != -1
302-
else { throw POSIXError.fromErrno! }
298+
else { throw POSIXError.fromErrno() }
303299

304300
return fdCount > 0
305301
}
@@ -309,12 +305,12 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
309305
var flags = fcntl(internalSocket, F_GETFL, 0)
310306

311307
guard flags != -1
312-
else { throw POSIXError.fromErrno! }
308+
else { throw POSIXError.fromErrno() }
313309

314310
flags = fcntl(internalSocket, F_SETFL, flags | O_NONBLOCK);
315311

316312
guard flags != -1
317-
else { throw POSIXError.fromErrno! }
313+
else { throw POSIXError.fromErrno() }
318314
}
319315

320316
/// Write to the socket.
@@ -325,7 +321,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
325321
let actualByteCount = write(internalSocket, &buffer, buffer.count)
326322

327323
guard actualByteCount >= 0
328-
else { throw POSIXError.fromErrno! }
324+
else { throw POSIXError.fromErrno() }
329325

330326
guard actualByteCount == buffer.count
331327
else { throw L2CAPSocketError.sentLessBytes(actualByteCount) }
@@ -338,7 +334,7 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
338334
var optionLength = socklen_t(MemoryLayout<Options>.size)
339335

340336
guard getsockopt(internalSocket, SOL_L2CAP, L2CAP_OPTIONS, &optionValue, &optionLength) == 0
341-
else { throw POSIXError.fromErrno! }
337+
else { throw POSIXError.fromErrno() }
342338

343339
return optionValue
344340
}

Sources/BluetoothLinux/POSIXError.swift

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,26 @@ import Glibc
1717
internal extension POSIXError {
1818

1919
/// Creates error from C ```errno```.
20-
static var fromErrno: POSIXError? {
20+
static func fromErrno(file: StaticString = #file,
21+
line: UInt = #line,
22+
function: StaticString = #function) -> POSIXError {
2123

2224
guard let code = POSIXErrorCode(rawValue: errno)
23-
else { return nil }
25+
else { fatalError("Invalid POSIX Error \(errno)") }
2426

25-
return POSIXError(code)
27+
return POSIXError(code, function: function, file: file, line: line)
2628
}
2729

28-
init(_ code: POSIXErrorCode) {
29-
self.init(_nsError: NSPOSIXError(code))
30+
init(_ code: POSIXErrorCode,
31+
function: StaticString = #function,
32+
file: StaticString = #file,
33+
line: UInt = #line) {
34+
35+
self.init(_nsError: NSPOSIXError(code, function: function, file: file, line: line))
36+
}
37+
38+
var debugInformation: String? {
39+
return userInfo[NSPOSIXError.debugInformationKey] as? String
3040
}
3141
}
3242

@@ -47,7 +57,7 @@ extension POSIXError: CustomStringConvertible {
4757
}
4858

4959
#if os(macOS)
50-
extension POSIXErrorCode {
60+
extension POSIXErrorCode: CustomStringConvertible {
5161
public var description: String {
5262
return rawValue.description
5363
}
@@ -64,15 +74,26 @@ extension POSIXError: LocalizedError {
6474

6575
// MARK: - Supporting Types
6676

77+
/// NSError subclass for POSIX Errors
6778
internal final class NSPOSIXError: NSError {
6879

6980
let posixError: POSIXErrorCode
7081

71-
init(_ code: POSIXErrorCode) {
82+
init(_ code: POSIXErrorCode,
83+
function: StaticString = #function,
84+
file: StaticString = #file,
85+
line: UInt = #line) {
86+
87+
var userInfo = [String: Any](minimumCapacity: 1)
88+
userInfo[NSPOSIXError.debugInformationKey] = NSPOSIXError.debugInformation(
89+
function: function,
90+
file: file,
91+
line: line)
92+
7293
self.posixError = code
7394
super.init(domain: NSPOSIXErrorDomain,
7495
code: Int(code.rawValue),
75-
userInfo: nil)
96+
userInfo: userInfo)
7697
}
7798

7899
required init?(coder decoder: NSCoder) {
@@ -96,6 +117,30 @@ internal final class NSPOSIXError: NSError {
96117
override var description: String {
97118
return "\(posixError.errorMessage) (\(posixError))"
98119
}
120+
121+
var debugInformation: String? {
122+
return userInfo[NSPOSIXError.debugInformationKey] as? String
123+
}
124+
}
125+
126+
internal extension NSPOSIXError {
127+
128+
/// Contains
129+
static let debugInformationKey: String = "NSPOSIXErrorDebugInformation"
130+
}
131+
132+
private extension NSPOSIXError {
133+
134+
static let module = NSStringFromClass(NSPOSIXError.self).components(separatedBy:".")[0]
135+
136+
static func debugInformation(function: StaticString,
137+
file: StaticString,
138+
line: UInt) -> String {
139+
140+
let file = "\(file)"
141+
let fileName = file.components(separatedBy: "/").last ?? file
142+
return "\(module):\(fileName):\(function):\(line)"
143+
}
99144
}
100145

101146
#if !swift(>=5.1) && (os(Linux) || os(Android))

0 commit comments

Comments
 (0)