Skip to content

Commit 5bfaf21

Browse files
authored
fix(datastore): improve sync event error handling - cannotParseResponse (#2536)
1 parent 84d3b9e commit 5bfaf21

File tree

4 files changed

+364
-100
lines changed

4 files changed

+364
-100
lines changed

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Sync/MutationSync/OutgoingMutationQueue/ProcessMutationErrorFromCloudOperation.swift

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,42 @@ class ProcessMutationErrorFromCloudOperation: AsynchronousOperation {
5454
return
5555
}
5656

57-
if let apiError = apiError, isAuthSignedOutError(apiError: apiError) {
57+
if let apiError = apiError {
58+
if isAuthSignedOutError(apiError: apiError) {
59+
log.verbose("User is signed out, passing error back to the error handler, and removing mutation event.")
60+
} else if let underlyingError = apiError.underlyingError {
61+
log.debug("Received APIError: \(apiError.localizedDescription) with underlying error: \(underlyingError.localizedDescription)")
62+
} else {
63+
log.debug("Received APIError: \(apiError.localizedDescription)")
64+
}
5865
dataStoreConfiguration.errorHandler(DataStoreError.api(apiError, mutationEvent))
5966
finish(result: .success(nil))
6067
return
6168
}
6269

63-
guard let graphQLResponseError = graphQLResponseError,
64-
case let .error(graphQLErrors) = graphQLResponseError else {
65-
finish(result: .success(nil))
66-
return
70+
guard let graphQLResponseError = graphQLResponseError else {
71+
dataStoreConfiguration.errorHandler(
72+
DataStoreError.api(APIError.unknown("This is unexpected. Missing APIError and GraphQLError.", ""),
73+
mutationEvent))
74+
finish(result: .success(nil))
75+
return
76+
}
77+
78+
guard case let .error(graphQLErrors) = graphQLResponseError else {
79+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
80+
finish(result: .success(nil))
81+
return
6782
}
6883

6984
guard graphQLErrors.count == 1 else {
7085
log.error("Received more than one error response: \(String(describing: graphQLResponseError))")
86+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
7187
finish(result: .success(nil))
7288
return
7389
}
7490

7591
guard let graphQLError = graphQLErrors.first else {
92+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
7693
finish(result: .success(nil))
7794
return
7895
}
@@ -84,22 +101,26 @@ class ProcessMutationErrorFromCloudOperation: AsynchronousOperation {
84101
let payload = HubPayload(eventName: HubPayload.EventName.DataStore.conditionalSaveFailed,
85102
data: mutationEvent)
86103
Amplify.Hub.dispatch(to: .dataStore, payload: payload)
104+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
87105
finish(result: .success(nil))
88106
case .conflictUnhandled:
89107
processConflictUnhandled(extensions)
90108
case .unauthorized:
91-
// TODO: dispatch Hub event
92109
log.debug("Unauthorized mutation \(errorType)")
110+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
93111
finish(result: .success(nil))
94112
case .operationDisabled:
95113
log.debug("Operation disabled \(errorType)")
114+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
96115
finish(result: .success(nil))
97116
case .unknown(let errorType):
98117
log.debug("Unhandled error with errorType \(errorType)")
118+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
99119
finish(result: .success(nil))
100120
}
101121
} else {
102122
log.debug("GraphQLError missing extensions and errorType \(graphQLError)")
123+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
103124
finish(result: .success(nil))
104125
}
105126
}

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Sync/RequestRetryablePolicy.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class RequestRetryablePolicy: RequestRetryable {
3838
.cannotConnectToHost,
3939
.cannotFindHost,
4040
.timedOut,
41-
.dataNotAllowed:
41+
.dataNotAllowed,
42+
.cannotParseResponse:
4243
let waitMillis = retryDelayInMillseconds(for: attemptNumber)
4344
return RequestRetryAdvice(shouldRetry: true, retryInterval: .milliseconds(waitMillis))
4445
default:

0 commit comments

Comments
 (0)