diff --git a/infra/event-sync/src/main/java/com/simprints/infra/eventsync/sync/down/tasks/EventDownSyncTask.kt b/infra/event-sync/src/main/java/com/simprints/infra/eventsync/sync/down/tasks/EventDownSyncTask.kt index 15ef988505..2adc73e79d 100644 --- a/infra/event-sync/src/main/java/com/simprints/infra/eventsync/sync/down/tasks/EventDownSyncTask.kt +++ b/infra/event-sync/src/main/java/com/simprints/infra/eventsync/sync/down/tasks/EventDownSyncTask.kt @@ -75,7 +75,7 @@ internal class EventDownSyncTask @Inject constructor( .catch { // Track a case when event stream is closed due to a parser error, // but the exception is handled gracefully and channel is closed correctly. - errorType = it.toString() + errorType = it.javaClass.simpleName } .collect { batchOfEventsToProcess.add(it) @@ -105,7 +105,7 @@ internal class EventDownSyncTask @Inject constructor( } Simber.d(t) - errorType = t.toString() + errorType = t.javaClass.simpleName lastOperation = processBatchedEvents(operation, batchOfEventsToProcess, lastOperation) emitProgress(lastOperation, count, count) diff --git a/infra/event-sync/src/main/java/com/simprints/infra/eventsync/sync/up/tasks/EventUpSyncTask.kt b/infra/event-sync/src/main/java/com/simprints/infra/eventsync/sync/up/tasks/EventUpSyncTask.kt index 9deaee8292..456bbcde1c 100644 --- a/infra/event-sync/src/main/java/com/simprints/infra/eventsync/sync/up/tasks/EventUpSyncTask.kt +++ b/infra/event-sync/src/main/java/com/simprints/infra/eventsync/sync/up/tasks/EventUpSyncTask.kt @@ -295,7 +295,7 @@ internal class EventUpSyncTask @Inject constructor( endedAt = timeHelper.now(), requestId = requestId, responseStatus = result?.status, - errorType = ex.toString(), + errorType = ex.javaClass.simpleName, ) ) } diff --git a/infra/event-sync/src/test/java/com/simprints/infra/eventsync/sync/down/tasks/EventDownSyncTaskTest.kt b/infra/event-sync/src/test/java/com/simprints/infra/eventsync/sync/down/tasks/EventDownSyncTaskTest.kt index bb9eec9464..afba66c80d 100644 --- a/infra/event-sync/src/test/java/com/simprints/infra/eventsync/sync/down/tasks/EventDownSyncTaskTest.kt +++ b/infra/event-sync/src/test/java/com/simprints/infra/eventsync/sync/down/tasks/EventDownSyncTaskTest.kt @@ -41,6 +41,7 @@ import org.junit.Before import org.junit.Rule import org.junit.Test import java.util.UUID +import kotlin.coroutines.cancellation.CancellationException class EventDownSyncTaskTest { @@ -241,6 +242,48 @@ class EventDownSyncTaskTest { coVerify { enrolmentRecordRepository.performActions(listOf(Deletion(event.payload.subjectId))) } } + @Test + fun downSync_shouldAddEventWithExceptionClassSimpleNameIfDownloadFails() = runTest { + val expectedException = Exception("Test") + coEvery { eventRemoteDataSource.getEvents(any(), any(), any()) } throws expectedException + + eventDownSyncTask.downSync(this, projectOp, eventScope).toList() + + coVerify(exactly = 1) { + eventRepository.addOrUpdateEvent( + eventScope, + match { + it is EventDownSyncRequestEvent && + it.payload.errorType == expectedException.javaClass.simpleName + } + ) + } + } + + @Test + fun downSync_shouldAddEventWithExceptionClassSimpleNameIfEventStreamFails() = runTest { + val expectedException = CancellationException("Test") + downloadEventsChannel = Channel(capacity = Channel.UNLIMITED) + coEvery { eventRemoteDataSource.getEvents(any(), any(), any()) } returns EventDownSyncResult( + 0, + status = 200, + downloadEventsChannel + ) + downloadEventsChannel.cancel(expectedException) + + eventDownSyncTask.downSync(this, projectOp, eventScope).toList() + + coVerify(exactly = 1) { + eventRepository.addOrUpdateEvent( + eventScope, + match { + it is EventDownSyncRequestEvent && + it.payload.errorType == expectedException.javaClass.simpleName + } + ) + } + } + @Test fun moveSubjectFromModulesUnderSyncing_theOriginalModuleSyncShouldDoNothing() = runTest { val eventToMoveToModule2 = ENROLMENT_RECORD_MOVE diff --git a/infra/event-sync/src/test/java/com/simprints/infra/eventsync/sync/up/tasks/EventUpSyncTaskTest.kt b/infra/event-sync/src/test/java/com/simprints/infra/eventsync/sync/up/tasks/EventUpSyncTaskTest.kt index dda483eb95..f160eab421 100644 --- a/infra/event-sync/src/test/java/com/simprints/infra/eventsync/sync/up/tasks/EventUpSyncTaskTest.kt +++ b/infra/event-sync/src/test/java/com/simprints/infra/eventsync/sync/up/tasks/EventUpSyncTaskTest.kt @@ -458,6 +458,30 @@ internal class EventUpSyncTaskTest { } } + @Test + fun `upSync should add event with exception class simple name if upload fails`() = runTest { + setUpSyncKind(UpSynchronizationConfiguration.UpSynchronizationKind.ALL) + + coEvery { eventRepo.getClosedEventScopes(any()) } returns emptyList() + coEvery { eventRepo.getClosedEventScopes(EventScopeType.SESSION) } returns listOf( + createSessionScope(GUID1), + ) + coEvery { + eventRepo.getEventsFromScope(any()) + } returns listOf(createEventWithSessionId(GUID1, GUID1)) + val expectedException = NetworkConnectionException("Test", Throwable()) + coEvery { eventRemoteDataSource.post(any(), any(), any()) } throws expectedException + + eventUpSyncTask.upSync(operation, eventScope).toList() + + coVerify(exactly = 1) { + eventRepo.addOrUpdateEvent(any(), match { + it is EventUpSyncRequestEvent && + it.payload.errorType == expectedException.javaClass.simpleName + }) + } + } + @Test fun `upload should save request event for each upload request`() = runTest { setUpSyncKind(UpSynchronizationConfiguration.UpSynchronizationKind.ALL)