Skip to content

Commit 4b41745

Browse files
committed
Add a SyncJobStatus result callback amid Sync retries
1 parent 8301d62 commit 4b41745

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

engine/src/main/java/com/google/android/fhir/sync/FhirSyncWorker.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import androidx.work.CoroutineWorker
2121
import androidx.work.Data
2222
import androidx.work.WorkerParameters
2323
import androidx.work.workDataOf
24+
import ca.uhn.fhir.context.FhirContext
2425
import com.google.android.fhir.FhirEngine
2526
import com.google.android.fhir.FhirEngineProvider
2627
import com.google.android.fhir.OffsetDateTimeTypeAdapter
@@ -32,11 +33,15 @@ import com.google.android.fhir.sync.upload.request.UploadRequestGeneratorFactory
3233
import com.google.gson.ExclusionStrategy
3334
import com.google.gson.FieldAttributes
3435
import com.google.gson.GsonBuilder
36+
import java.nio.charset.StandardCharsets
3537
import java.time.OffsetDateTime
3638
import kotlinx.coroutines.CoroutineScope
3739
import kotlinx.coroutines.Dispatchers
3840
import kotlinx.coroutines.cancel
3941
import kotlinx.coroutines.launch
42+
import org.apache.commons.io.IOUtils
43+
import org.hl7.fhir.r4.model.OperationOutcome
44+
import retrofit2.HttpException
4045
import timber.log.Timber
4146

4247
/**
@@ -135,6 +140,8 @@ abstract class FhirSyncWorker(appContext: Context, workerParams: WorkerParameter
135140
}
136141

137142
val result = synchronizer.synchronize()
143+
if (result is SyncJobStatus.Failed) onFailedSyncJobResult(result)
144+
138145
val output = buildWorkData(result)
139146

140147
// await/join is needed to collect states completely
@@ -155,6 +162,34 @@ abstract class FhirSyncWorker(appContext: Context, workerParams: WorkerParameter
155162
}
156163
}
157164

165+
open fun onFailedSyncJobResult(failedSyncJobStatus: SyncJobStatus.Failed) {
166+
try {
167+
val jsonParser = FhirContext.forR4().newJsonParser()
168+
val exceptions = (failedSyncJobStatus).exceptions
169+
170+
exceptions.forEach { resourceSyncException ->
171+
val operationOutcome =
172+
jsonParser.parseResource(
173+
IOUtils.toString(
174+
(resourceSyncException.exception as HttpException)
175+
.response()
176+
?.errorBody()
177+
?.byteStream(),
178+
StandardCharsets.UTF_8,
179+
),
180+
) as OperationOutcome
181+
182+
operationOutcome.issue.forEach { operationOutcomeIssueComponent ->
183+
Timber.e(
184+
"SERVER ${operationOutcomeIssueComponent.severity} - HTTP ${resourceSyncException.exception.code()} | Code - ${operationOutcomeIssueComponent.code} | Diagnostics - ${operationOutcomeIssueComponent.diagnostics}",
185+
)
186+
}
187+
}
188+
} catch (e: Exception) {
189+
Timber.e(e)
190+
}
191+
}
192+
158193
private fun buildWorkData(state: SyncJobStatus): Data {
159194
return workDataOf(
160195
// send serialized state and type so that consumer can convert it back

0 commit comments

Comments
 (0)