Skip to content

Commit 133a1fa

Browse files
authored
fix(datastore): improve sync event error handling - cannotParseResponse (#2532)
1 parent 3e6fbf8 commit 133a1fa

File tree

4 files changed

+363
-99
lines changed

4 files changed

+363
-99
lines changed

AmplifyPlugins/DataStore/AWSDataStoreCategoryPlugin/Sync/MutationSync/OutgoingMutationQueue/ProcessMutationErrorFromCloudOperation.swift

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

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

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

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

7692
guard let graphQLError = graphQLErrors.first else {
93+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
7794
finish(result: .success(nil))
7895
return
7996
}
@@ -85,22 +102,26 @@ class ProcessMutationErrorFromCloudOperation: AsynchronousOperation {
85102
let payload = HubPayload(eventName: HubPayload.EventName.DataStore.conditionalSaveFailed,
86103
data: mutationEvent)
87104
Amplify.Hub.dispatch(to: .dataStore, payload: payload)
105+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
88106
finish(result: .success(nil))
89107
case .conflictUnhandled:
90108
processConflictUnhandled(extensions)
91109
case .unauthorized:
92-
// TODO: dispatch Hub event
93110
log.debug("Unauthorized mutation \(errorType)")
111+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
94112
finish(result: .success(nil))
95113
case .operationDisabled:
96114
log.debug("Operation disabled \(errorType)")
115+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
97116
finish(result: .success(nil))
98117
case .unknown(let errorType):
99118
log.debug("Unhandled error with errorType \(errorType)")
119+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
100120
finish(result: .success(nil))
101121
}
102122
} else {
103123
log.debug("GraphQLError missing extensions and errorType \(graphQLError)")
124+
dataStoreConfiguration.errorHandler(DataStoreError.api(graphQLResponseError, mutationEvent))
104125
finish(result: .success(nil))
105126
}
106127
}

AmplifyPlugins/DataStore/AWSDataStoreCategoryPlugin/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)