Skip to content

Commit

Permalink
Updated POSIXError
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Apr 19, 2019
1 parent 2e3c8ff commit bfa4956
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
24 changes: 17 additions & 7 deletions Sources/BluetoothLinux/POSIXError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ internal extension POSIXError {
}
}

internal extension POSIXErrorCode {
var errorMessage: String {
return String(cString: strerror(rawValue), encoding: .utf8)!
}
}

// MARK: - CustomStringConvertible

// https://github.com/apple/swift/pull/24149
Expand All @@ -36,6 +42,14 @@ extension POSIXError: CustomStringConvertible {
}
}

// MARK: - LocalizedError

extension POSIXError: LocalizedError {
public var localizedDescription: String {
return POSIXErrorCode(rawValue: Int32(errorCode))?.errorMessage ?? "Invalid error \(errorCode)"
}
}

// MARK: - Supporting Types

internal final class NSPOSIXError: NSError {
Expand All @@ -60,19 +74,15 @@ internal final class NSPOSIXError: NSError {
}

override var localizedDescription: String {
return errorMessage
return posixError.errorMessage
}

override var localizedFailureReason: String? {
return errorMessage
}

internal var errorMessage: String {
return String(cString: strerror(posixError.rawValue), encoding: .utf8)!
return posixError.errorMessage
}

override var description: String {
return "Error Domain=\(domain) Code=\(code) \"\(errorMessage)\""
return "Error Domain=\(domain) Code=\(code) \"\(posixError.errorMessage)\""
}
}

Expand Down
14 changes: 8 additions & 6 deletions Tests/BluetoothLinuxTests/BluetoothLinuxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,23 @@ final class BluetoothLinuxTests: XCTestCase {
for (errorCode, string) in errors {

let error = POSIXError(_nsError: NSPOSIXError(errorCode))

#if os(macOS) || swift(>=5.1)
// https://github.com/apple/swift-corelibs-foundation/pull/2113
XCTAssertEqual(error.code, errorCode)
#endif
#if os(macOS)
// https://github.com/apple/swift-corelibs-foundation/pull/2140
XCTAssertEqual(error.localizedDescription, string)
#endif
XCTAssertEqual(error.errorCode, Int(errorCode.rawValue))
XCTAssertEqual(error._nsError.localizedFailureReason, string)
XCTAssertEqual(error._nsError.localizedDescription, string)
XCTAssertEqual(error._nsError.domain, NSPOSIXErrorDomain)
XCTAssertEqual(error._nsError.code, Int(errorCode.rawValue))
let errorDescription = "Error Domain=\(NSPOSIXErrorDomain) Code=\(errorCode.rawValue) \"\(string)\""
XCTAssertEqual("\(error)", errorDescription)

do { throw error } // deal with protocol and not concrete type
catch {
XCTAssertEqual(error.localizedDescription, string)
let errorDescription = "Error Domain=\(NSPOSIXErrorDomain) Code=\(errorCode.rawValue) \"\(string)\""
XCTAssertEqual("\(error)", errorDescription)
}
}
}
}

0 comments on commit bfa4956

Please sign in to comment.