Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
5d committed Mar 15, 2024
1 parent b986835 commit 18e2bab
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand All @@ -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
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<State, Never>()

Expand All @@ -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)
}
Expand Down

0 comments on commit 18e2bab

Please sign in to comment.