Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datastore): Continue initial sync if atleast one model syncs successfully and other models fail due to Unauthorized API error #3224

Merged
merged 6 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ final class AWSInitialSyncOrchestrator: InitialSyncOrchestrator {
allMessages.joined(separator: "\n"),
underlyingError
)

return .failure(syncError)
}

Expand Down Expand Up @@ -245,6 +246,15 @@ extension AWSInitialSyncOrchestrator {
case .unauthorized = AppSyncErrorType(errorTypeValue) {
return true
}

// Check is API error is of unauthorized type
if case let .api(amplifyError, _) = datastoreError,
let apiError = amplifyError as? APIError {
if apiError.isUnauthorized() {
return true
}
thisisabhash marked this conversation as resolved.
Show resolved Hide resolved
}

return false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Amplify

extension APIError {

static let UnauthorizedMessageString: String = "Unauthorized"

/// By default, this consolidates unauthorized error scenarios coming from `APIError.httpStatusError` and
/// `APIError.operationError`. For `.httpStatusError`, this checks if the status code is 401 or 403. For
/// `.operationError`, this checks if the error description contains "Unauthorized".
///
/// **Warning** Customized server responses that indicate unauthorized may not match the internal mapping done
/// in this API and return `false`. Check APIError enum directly or create your own `UnauthorizedDeterming` rule,
/// for example:
/// ```
/// static let customRule = UnauthorizedDetermining { error in
/// // Your custom logic to determine if `error` is unauthorized.
/// // return `true` or `false`.
/// }
/// ```
///
/// - Parameter rule: Used to determine if the `APIError` is an unauthorized error.
/// - Returns: `true` if unauthorized error, `false` otherwise
public func isUnauthorized(rule: UnauthorizedDetermining = .default) -> Bool {
rule.isUnauthorized(self)
}

public struct UnauthorizedDetermining {
let isUnauthorized: (APIError) -> Bool
thisisabhash marked this conversation as resolved.
Show resolved Hide resolved

public init(isUnauthenticated: @escaping (APIError) -> Bool) {
self.isUnauthorized = isUnauthenticated
}

public static let `default` = UnauthorizedDetermining { error in
error.containsUnauthorizedMessageString || error.is401or403
}
}

private var is401or403: Bool {
switch self {
case .httpStatusError(let statusCode, _):
return statusCode == 401 || statusCode == 403
default:
return false
}
}

private var containsUnauthorizedMessageString: Bool {
switch self {
case .operationError(let errorDescription, _, _):
return errorDescription.range(of: APIError.UnauthorizedMessageString, options: .caseInsensitive) != nil
default: return false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class InitialSyncOrchestratorTests: XCTestCase {
Amplify.Hub.removeListener(hubListener)
sink.cancel()
}

/// - Given: An InitialSyncOrchestrator with a model dependency graph, API is expected to return an error for certain models
/// - When:
/// - The orchestrator starts up
Expand Down
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/aws-amplify/aws-appsync-realtime-client-ios.git",
"state" : {
"revision" : "c7ec93dcbbcd8abc90c74203937f207a7fcaa611",
"version" : "3.1.1"
"revision" : "b036e83716789c13a3480eeb292b70caa54114f2",
"version" : "3.1.0"
}
thisisabhash marked this conversation as resolved.
Show resolved Hide resolved
},
{
Expand Down Expand Up @@ -95,8 +95,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "532d8b529501fb73a2455b179e0bbb6d49b652ed",
"version" : "1.5.3"
"revision" : "32e8d724467f8fe623624570367e3d50c5638e46",
"version" : "1.5.2"
}
},
{
Expand Down
Loading