Skip to content

Commit 66466d7

Browse files
committed
1 parent 8863c21 commit 66466d7

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Sources/BluetoothLinux/POSIXError.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,42 @@
77
//
88

99
import Foundation
10+
import Bluetooth
11+
12+
#if canImport(MSVCRT)
13+
import MSVCRT
14+
import WinSDK
15+
#elseif canImport(Darwin)
16+
import Darwin
17+
#elseif canImport(Glibc)
18+
import Glibc
19+
#endif
1020

1121
internal extension POSIXError {
1222

1323
/// Creates error from C ```errno```.
1424
static var fromErrno: POSIXError? {
1525

16-
guard let code = POSIXError.Code(rawValue: POSIXError.Code.RawValue(errno))
26+
guard let code = POSIXErrorCode(rawValue: errno)
1727
else { return nil }
1828

1929
return self.init(code)
2030
}
31+
32+
/// Initializes `POSIXError` from an error code.
33+
init(_ errorCode: POSIXErrorCode) {
34+
35+
var userInfo = [String: Any](minimumCapacity: 1)
36+
37+
if let description = String(cString: strerror(errorCode.rawValue), encoding: .ascii) {
38+
userInfo[NSLocalizedDescriptionKey] = description
39+
}
40+
41+
let nsError = NSError(
42+
domain: NSPOSIXErrorDomain,
43+
code: Int(errorCode.rawValue),
44+
userInfo: userInfo)
45+
46+
self.init(_nsError: nsError)
47+
}
2148
}

0 commit comments

Comments
 (0)