diff --git a/Sources/NIO/IO.swift b/Sources/NIO/IO.swift index f41fd68f5..f725627fc 100644 --- a/Sources/NIO/IO.swift +++ b/Sources/NIO/IO.swift @@ -19,33 +19,40 @@ import Darwin /// An `Error` for an IO operation. public struct IOError: Swift.Error { - + public enum FailureDescription { + case function(StaticString) + case reason(String) + } /// The `errno` that was set for the operation. public let errnoCode: Int32 // TODO: Fix me to lazy create /// The actual reason (in an human-readable form) for this `IOError`. - public let reason: String + public let reason: FailureDescription /// Creates a new `IOError`` /// + /// - note: At the moment, this constructor is more expensive than `IOError(errnoCode:function:)` as the `String` will incur reference counting + /// /// - parameters: /// - errorCode: the `errno` that was set for the operation. /// - reason: the actual reason (in an human-readable form). public init(errnoCode: Int32, reason: String) { self.errnoCode = errnoCode - self.reason = reason + self.reason = .reason(reason) } -} -/// Creates a new IOError for a function call -/// -/// - parameters: -/// - errorCode: the `errno` that was set for the operation. -/// - function: the function / syscall that caused the error. -/// - returns: error that was created. -func ioError(errnoCode: Int32, function: String) -> IOError { - return IOError(errnoCode: errnoCode, reason: reasonForError(errnoCode: errno, function: function)) + /// Creates a new `IOError`` + /// + /// - note: This constructor is the cheapest way to create an `IOError`. + /// + /// - parameters: + /// - errorCode: the `errno` that was set for the operation. + /// - function: The function the error happened in, the human readable description will be generated automatically when needed. + public init(errnoCode: Int32, function: StaticString) { + self.errnoCode = errnoCode + self.reason = .function(function) + } } /// Returns a reason to use when constructing a `IOError`. @@ -54,7 +61,7 @@ func ioError(errnoCode: Int32, function: String) -> IOError { /// - errorCode: the `errno` that was set for the operation. /// - function: the function / syscall that caused the error. /// -returns: the constructed reason. -private func reasonForError(errnoCode: Int32, function: String) -> String { +private func reasonForError(errnoCode: Int32, function: StaticString) -> String { if let errorDescC = strerror(errnoCode) { let errorDescLen = strlen(errorDescC) return errorDescC.withMemoryRebound(to: UInt8.self, capacity: errorDescLen) { ptr in @@ -66,9 +73,18 @@ private func reasonForError(errnoCode: Int32, function: String) -> String { } } -extension IOError { +extension IOError: CustomStringConvertible { + public var description: String { + return self.localizedDescription + } + public var localizedDescription: String { - return self.reason + switch self.reason { + case .reason(let reason): + return reason + case .function(let function): + return reasonForError(errnoCode: self.errnoCode, function: function) + } } } diff --git a/Sources/NIO/Socket.swift b/Sources/NIO/Socket.swift index 6e62d6f6f..e830243c2 100644 --- a/Sources/NIO/Socket.swift +++ b/Sources/NIO/Socket.swift @@ -66,7 +66,7 @@ final class Socket : BaseSocket { func finishConnect() throws { let result: Int32 = try getOption(level: SOL_SOCKET, name: SO_ERROR) if result != 0 { - throw ioError(errnoCode: result, function: "getsockopt") + throw IOError(errnoCode: result, function: "getsockopt") } } diff --git a/Sources/NIO/System.swift b/Sources/NIO/System.swift index a3b628acd..44436e770 100644 --- a/Sources/NIO/System.swift +++ b/Sources/NIO/System.swift @@ -37,7 +37,7 @@ private func isBlacklistedErrno(_ code: Int32) -> Bool { /* Sorry, we really try hard to not use underscored attributes. In this case however we seem to break the inlining threshold which makes a system call take twice the time, ie. we need this exception. */ @inline(__always) -private func wrapSyscallMayBlock(_ fn: () throws -> T , where: StaticString = #function) throws -> IOResult { +private func wrapSyscallMayBlock(_ fn: () throws -> T , where function: StaticString = #function) throws -> IOResult { while true { let res = try fn() if res == -1 { @@ -49,7 +49,7 @@ private func wrapSyscallMayBlock(_ fn: () throws -> T , wh return .wouldBlock(0) default: assert(!isBlacklistedErrno(err), "blacklisted errno \(err) \(strerror(err)!)") - throw ioError(errnoCode: err, function: `where`.withUTF8Buffer { String(decoding: $0, as: UTF8.self) }) + throw IOError(errnoCode: err, function: function) } } @@ -59,7 +59,7 @@ private func wrapSyscallMayBlock(_ fn: () throws -> T , wh /* Sorry, we really try hard to not use underscored attributes. In this case however we seem to break the inlining threshold which makes a system call take twice the time, ie. we need this exception. */ @inline(__always) -private func wrapSyscall(_ fn: () throws -> T, where: StaticString = #function) throws -> T { +private func wrapSyscall(_ fn: () throws -> T, where function: StaticString = #function) throws -> T { while true { let res = try fn() if res == -1 { @@ -68,7 +68,7 @@ private func wrapSyscall(_ fn: () throws -> T, where: Stat continue } assert(!isBlacklistedErrno(err), "blacklisted errno \(err) \(strerror(err)!)") - throw ioError(errnoCode: err, function: `where`.withUTF8Buffer { String(decoding: $0, as: UTF8.self) }) + throw IOError(errnoCode: err, function: function) } return res } diff --git a/Tests/NIOTests/SystemTest.swift b/Tests/NIOTests/SystemTest.swift index 716020e36..5f8bde01c 100644 --- a/Tests/NIOTests/SystemTest.swift +++ b/Tests/NIOTests/SystemTest.swift @@ -37,7 +37,8 @@ class SystemTest: XCTestCase { XCTFail("success even though the call was invalid") } catch let e as IOError { XCTAssertEqual(ENOTSOCK, e.errnoCode) - XCTAssert(e.reason.contains("\(ENOTSOCK)")) + XCTAssert(e.description.contains("setsockopt")) + XCTAssert(e.description.contains("\(ENOTSOCK)")) XCTAssert(e.localizedDescription.contains("\(ENOTSOCK)"), "\(e.localizedDescription)") } catch let e { XCTFail("wrong error thrown: \(e)")