Fix handling Google Drive 404 errors

pull/1244/head
Alex Baker 5 years ago
parent d74f041ef6
commit 7bfd339021

@ -9,6 +9,7 @@ import com.google.api.services.drive.Drive
import com.google.api.services.drive.model.File
import com.todoroo.astrid.backup.BackupConstants
import com.todoroo.astrid.gtasks.api.HttpCredentialsAdapter
import com.todoroo.astrid.gtasks.api.HttpNotFoundException
import dagger.hilt.android.qualifiers.ApplicationContext
import org.tasks.DebugNetworkInterceptor
import org.tasks.files.FileHelper
@ -28,13 +29,18 @@ class DriveInvoker(
.build()
@Throws(IOException::class)
suspend fun getFile(folderId: String?): File? {
return execute(service.files()[folderId].setFields("id, trashed"))
suspend fun getFile(folderId: String?): File? = try {
execute(service.files()[folderId].setFields("id, trashed"))
} catch (ignored: HttpNotFoundException) {
null
}
@Throws(IOException::class)
suspend fun delete(file: File) {
execute(service.files().delete(file.id))
try {
execute(service.files().delete(file.id))
} catch (ignored: HttpNotFoundException) {
}
}
@Throws(IOException::class)

@ -48,17 +48,7 @@ class DriveUploader @WorkerInject constructor(
drive
.getFilesByPrefix(folder.id, "auto.")
.drop(BackupWork.DAYS_TO_KEEP_BACKUP)
.forEach {
try {
drive.delete(it)
} catch (e: GoogleJsonResponseException) {
if (e.statusCode == 404) {
Timber.e(e)
} else {
throw e
}
}
}
.forEach { drive.delete(it) }
}
Result.success()
} catch (e: SocketTimeoutException) {
@ -102,13 +92,7 @@ class DriveUploader @WorkerInject constructor(
val folderId = preferences.getStringValue(R.string.p_google_drive_backup_folder)
var file: File? = null
if (!isNullOrEmpty(folderId)) {
try {
file = drive.getFile(folderId)
} catch (e: GoogleJsonResponseException) {
if (e.statusCode != 404) {
throw e
}
}
file = drive.getFile(folderId)
}
return if (file == null || file.trashed) drive.createFolder(FOLDER_NAME) else file
}

Loading…
Cancel
Save