Skip to content

Commit ed2827f

Browse files
MohamadJaaratmspzz
andauthored
fix: try catch migration errors and include the report when failing (#1616)
Co-authored-by: Tommaso Piazza <196761+tmspzz@users.noreply.github.com>
1 parent 4ebb7f4 commit ed2827f

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

app/src/main/kotlin/com/wire/android/migration/MigrationManager.kt

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,11 @@ class MigrationManager @Inject constructor(
115115
userId: UserId,
116116
coroutineScope: CoroutineScope,
117117
updateProgress: suspend (MigrationData.Progress) -> Unit,
118-
): MigrationData.Result =
119-
try {
120-
val report: MigrationReport = MigrationReport()
121-
updateProgress(MigrationData.Progress(MigrationData.Progress.Type.MESSAGES))
118+
): MigrationData.Result {
119+
val report: MigrationReport = MigrationReport()
120+
updateProgress(MigrationData.Progress(MigrationData.Progress.Type.MESSAGES))
121+
122+
return try {
122123
appLogger.d("$TAG - Step 1 - Migrating users for ${userId.value.obfuscateId()}")
123124
migrateUsers(userId)
124125
.also { report.addUserReport(userId, it) }
@@ -152,8 +153,12 @@ class MigrationManager @Inject constructor(
152153
} catch (e: Exception) {
153154
globalDataStore.setUserMigrationStatus(userId.value, UserMigrationStatus.CompletedWithErrors)
154155
appLogger.e("$TAG - Migration failed for ${userId.value.obfuscateId()}")
155-
throw e
156+
MigrationData.Result.Failure.Messages(
157+
CoreFailure.Unknown(e).getErrorCode().toString(),
158+
report.toFormattedString()
159+
)
156160
}
161+
}
157162

158163
suspend fun migrate(
159164
coroutineScope: CoroutineScope,
@@ -259,32 +264,37 @@ class MigrationManager @Inject constructor(
259264

260265
val migrationJobs: List<Job> = migratedAccounts.map { userId ->
261266
coroutineScope.launch(migrationDispatcher) {
262-
appLogger.d("$TAG - Step 2 - Migrating clients for ${userId.value.obfuscateId()}")
263-
migrateClientsData(userId, isFederated)
264-
.also { report.addClientReport(userId, it) }
265-
appLogger.d("$TAG - Step 3 - Migrating users for ${userId.value.obfuscateId()}")
266-
migrateUsers(userId)
267-
.also { report.addUserReport(userId, it) }
268-
.flatMap {
269-
appLogger.d("$TAG - Step 4 - Migrating conversations for ${userId.value.obfuscateId()}")
270-
migrateConversations(it)
271-
.also { report.addConversationReport(userId, it) }
272-
}.flatMap {
273-
appLogger.d("$TAG - Step 5 - Migrating messages for ${userId.value.obfuscateId()}")
274-
migrateMessages(userId, it, coroutineScope).let { failedConversations ->
275-
if (failedConversations.isEmpty()) {
276-
Either.Right(Unit)
277-
} else {
278-
Either.Left(failedConversations.values.first())
279-
}
280-
}.also { report.addMessagesReport(userId, it) }
281-
}.also {
282-
resultAcc[userId.value] = it
283-
}.onFailure {
284-
globalDataStore.setUserMigrationStatus(userId.value, UserMigrationStatus.CompletedWithErrors)
285-
}.onSuccess {
286-
globalDataStore.setUserMigrationStatus(userId.value, UserMigrationStatus.Successfully)
287-
}
267+
try {
268+
appLogger.d("$TAG - Step 2 - Migrating clients for ${userId.value.obfuscateId()}")
269+
migrateClientsData(userId, isFederated)
270+
.also { report.addClientReport(userId, it) }
271+
appLogger.d("$TAG - Step 3 - Migrating users for ${userId.value.obfuscateId()}")
272+
migrateUsers(userId)
273+
.also { report.addUserReport(userId, it) }
274+
.flatMap {
275+
appLogger.d("$TAG - Step 4 - Migrating conversations for ${userId.value.obfuscateId()}")
276+
migrateConversations(it)
277+
.also { report.addConversationReport(userId, it) }
278+
}.flatMap {
279+
appLogger.d("$TAG - Step 5 - Migrating messages for ${userId.value.obfuscateId()}")
280+
migrateMessages(userId, it, coroutineScope).let { failedConversations ->
281+
if (failedConversations.isEmpty()) {
282+
Either.Right(Unit)
283+
} else {
284+
Either.Left(failedConversations.values.first())
285+
}
286+
}.also { report.addMessagesReport(userId, it) }
287+
}.also {
288+
resultAcc[userId.value] = it
289+
}.onFailure {
290+
globalDataStore.setUserMigrationStatus(userId.value, UserMigrationStatus.CompletedWithErrors)
291+
}.onSuccess {
292+
globalDataStore.setUserMigrationStatus(userId.value, UserMigrationStatus.Successfully)
293+
}
294+
} catch (e: Exception) {
295+
resultAcc[userId.value] = Either.Left(CoreFailure.Unknown(e))
296+
globalDataStore.setUserMigrationStatus(userId.value, UserMigrationStatus.CompletedWithErrors)
297+
}
288298
}
289299
}
290300
migrationJobs.joinAll()

0 commit comments

Comments
 (0)