Skip to content

Commit c1a1ee6

Browse files
committed
errors as static let
1 parent 06a9183 commit c1a1ee6

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

Sources/NIOHTTPClient/SwiftNIOHTTP.swift

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,38 @@ public class HTTPClient {
199199
}
200200
}
201201

202-
public enum HTTPClientError: Error {
203-
case invalidURL
204-
case emptyHost
205-
case alreadyShutdown
206-
case emptyScheme
207-
case unsupportedScheme(String)
208-
case readTimeout
209-
case remoteConnectionClosed
210-
case cancelled
211-
case identityCodingIncorrectlyPresent
212-
case chunkedSpecifiedMultipleTimes
202+
public struct HTTPClientError: Error, Equatable, CustomStringConvertible {
203+
private enum Code: Equatable {
204+
case invalidURL
205+
case emptyHost
206+
case alreadyShutdown
207+
case emptyScheme
208+
case unsupportedScheme(String)
209+
case readTimeout
210+
case remoteConnectionClosed
211+
case cancelled
212+
case identityCodingIncorrectlyPresent
213+
case chunkedSpecifiedMultipleTimes
214+
}
215+
216+
private var code: Code
217+
218+
private init(code: Code) {
219+
self.code = code
220+
}
221+
222+
public var description: String {
223+
return "SandwichError.\(String(describing: self.code))"
224+
}
225+
226+
public static let invalidURL = HTTPClientError(code: .invalidURL)
227+
public static let emptyHost = HTTPClientError(code: .emptyHost)
228+
public static let alreadyShutdown = HTTPClientError(code: .alreadyShutdown)
229+
public static let emptyScheme = HTTPClientError(code: .emptyScheme)
230+
public static func unsupportedScheme(_ scheme: String) -> HTTPClientError { return HTTPClientError(code: .unsupportedScheme(scheme)) }
231+
public static let readTimeout = HTTPClientError(code: .readTimeout)
232+
public static let remoteConnectionClosed = HTTPClientError(code: .remoteConnectionClosed)
233+
public static let cancelled = HTTPClientError(code: .cancelled)
234+
public static let identityCodingIncorrectlyPresent = HTTPClientError(code: .identityCodingIncorrectlyPresent)
235+
public static let chunkedSpecifiedMultipleTimes = HTTPClientError(code: .chunkedSpecifiedMultipleTimes)
213236
}

Tests/NIOHTTPClientTests/SwiftNIOHTTPTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class SwiftHTTPTests: XCTestCase {
216216
}
217217

218218
XCTAssertThrowsError(try httpClient.get(url: "http://localhost:\(httpBin.port)/close").wait(), "Should fail") { error in
219-
guard case HTTPClientError.remoteConnectionClosed = error else {
219+
guard case let error = error as? HTTPClientError, error == .remoteConnectionClosed else {
220220
return XCTFail("Should fail with remoteConnectionClosed")
221221
}
222222
}
@@ -232,7 +232,7 @@ class SwiftHTTPTests: XCTestCase {
232232
}
233233

234234
XCTAssertThrowsError(try httpClient.get(url: "http://localhost:\(httpBin.port)/wait").wait(), "Should fail") { error in
235-
guard case HTTPClientError.readTimeout = error else {
235+
guard case let error = error as? HTTPClientError, error == .readTimeout else {
236236
return XCTFail("Should fail with readTimeout")
237237
}
238238
}
@@ -256,7 +256,7 @@ class SwiftHTTPTests: XCTestCase {
256256
}
257257

258258
XCTAssertThrowsError(try task.wait(), "Should fail") { error in
259-
guard case HTTPClientError.cancelled = error else {
259+
guard case let error = error as? HTTPClientError, error == .cancelled else {
260260
return XCTFail("Should fail with cancelled")
261261
}
262262
}

0 commit comments

Comments
 (0)