From 18e2bab8f5a665094ebae7b6fbe487616101e70b Mon Sep 17 00:00:00 2001 From: Di Wu Date: Fri, 15 Mar 2024 13:53:28 -0700 Subject: [PATCH] resolve comments --- .../AppSyncRealTimeClient.swift | 8 ++--- .../AppSyncRealTimeRequest.swift | 30 +++++++++++++++++-- .../AppSyncRealTimeClientTests.swift | 5 +++- .../AppSyncRealTimeRequestAuthTests.swift | 14 ++++----- .../WebSocket/AmplifyNetworkMonitor.swift | 10 ++----- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeClient.swift b/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeClient.swift index 1561769984..954f999a8f 100644 --- a/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeClient.swift +++ b/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeClient.swift @@ -295,16 +295,16 @@ actor AppSyncRealTimeClient: AppSyncRealTimeClientProtocol { } private static func decodeAppSyncRealTimeResponseError(_ data: JSONValue?) -> [Error] { - let appSyncRealTimeRequestErorrs = + let knownAppSyncRealTimeRequestErorrs = Self.decodeAppSyncRealTimeRequestError(data) - .filter { $0 != .unknown } - if appSyncRealTimeRequestErorrs.isEmpty { + .filter { $0.isUnknown } + if knownAppSyncRealTimeRequestErorrs.isEmpty { let graphQLErrors = Self.decodeGraphQLErrors(data) return graphQLErrors.isEmpty ? [APIError.operationError("Failed to decode AppSync error response", "", nil)] : graphQLErrors } else { - return appSyncRealTimeRequestErorrs + return knownAppSyncRealTimeRequestErorrs } } diff --git a/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeRequest.swift b/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeRequest.swift index 98cb6b7053..19599820b4 100644 --- a/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeRequest.swift +++ b/AmplifyPlugins/API/Sources/AWSAPIPlugin/AppSyncRealTimeClient/AppSyncRealTimeRequest.swift @@ -71,14 +71,34 @@ extension AppSyncRealTimeRequest: Encodable { extension AppSyncRealTimeRequest { - public enum Error: Swift.Error { + public enum Error: Swift.Error, Equatable { case timeout case limitExceeded case maxSubscriptionsReached case unauthorized - case unknown + case unknown(message: String? = nil, causedBy: Swift.Error? = nil, payload: [String: Any]?) + + var isUnknown: Bool { + if case .unknown = self { + return true + } + return false + } + + public static func == (lhs: AppSyncRealTimeRequest.Error, rhs: AppSyncRealTimeRequest.Error) -> Bool { + switch (lhs, rhs) { + case (.timeout, .timeout), + (.limitExceeded, .limitExceeded), + (.maxSubscriptionsReached, .maxSubscriptionsReached), + (.unauthorized, .unauthorized): + return true + default: + return false + } + } } + public static func parseResponseError( error: JSONValue ) -> AppSyncRealTimeRequest.Error? { @@ -98,7 +118,11 @@ extension AppSyncRealTimeRequest { case _ where errorType.contains(unauthorized): return .unauthorized default: - return .unknown + return .unknown( + message: error.message?.stringValue, + causedBy: nil, + payload: error.asObject + ) } } } diff --git a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeClientTests.swift b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeClientTests.swift index b4ef57a0e3..279ca304d3 100644 --- a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeClientTests.swift +++ b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeClientTests.swift @@ -171,7 +171,10 @@ class AppSyncRealTimeClientTests: XCTestCase { ) } catch { let requestError = error as! AppSyncRealTimeRequest.Error - XCTAssertEqual(requestError, .unknown) + guard case .unknown = requestError else { + XCTFail("The error should in unknown case") + return + } triggerUnknownErrorExpectation.fulfill() } } diff --git a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeRequestAuthTests.swift b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeRequestAuthTests.swift index 8978f82577..1755543d72 100644 --- a/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeRequestAuthTests.swift +++ b/AmplifyPlugins/API/Tests/AWSAPIPluginTests/AppSyncRealTimeClient/AppSyncRealTimeRequestAuthTests.swift @@ -9,14 +9,14 @@ import XCTest @testable import AWSAPIPlugin -let host = UUID().uuidString -let apiKey = UUID().uuidString -let date = UUID().uuidString -let id = UUID().uuidString -let data = UUID().uuidString -let token = UUID().uuidString - class AppSyncRealTimeRequestAuthTests: XCTestCase { + let host = UUID().uuidString + let apiKey = UUID().uuidString + let date = UUID().uuidString + let id = UUID().uuidString + let data = UUID().uuidString + let token = UUID().uuidString + var jsonEncoder = { let encoder = JSONEncoder() encoder.outputFormatting = [.sortedKeys] diff --git a/AmplifyPlugins/Core/AWSPluginsCore/WebSocket/AmplifyNetworkMonitor.swift b/AmplifyPlugins/Core/AWSPluginsCore/WebSocket/AmplifyNetworkMonitor.swift index 1bf1c2c2b1..23eb1ec4e2 100644 --- a/AmplifyPlugins/Core/AWSPluginsCore/WebSocket/AmplifyNetworkMonitor.swift +++ b/AmplifyPlugins/Core/AWSPluginsCore/WebSocket/AmplifyNetworkMonitor.swift @@ -18,11 +18,7 @@ public final class AmplifyNetworkMonitor { case offline } - #if DEBUG - private let monitor = NWPathMonitor(requiredInterfaceType: .cellular) - #else - private let monitor = NWPathMonitor() - #endif + private let monitor: NWPathMonitor private let subject = PassthroughSubject() @@ -32,8 +28,8 @@ public final class AmplifyNetworkMonitor { }.eraseToAnyPublisher() } - public init() { - + public init(on interface: NWInterface.InterfaceType? = nil) { + monitor = interface.map(NWPathMonitor.init(requiredInterfaceType:)) ?? NWPathMonitor() monitor.pathUpdateHandler = { [weak self] path in self?.subject.send(path.status == .satisfied ? .online : .offline) }