Better exception handling (#890)

* feat: Crash Fix for invalid secret key

* feat: add exception handling

* feat: spotless fixes
This commit is contained in:
Yogesh Choudhary Paliyal
2024-06-16 18:26:58 +05:30
committed by GitHub
parent 2b0194b0a0
commit ea8a4b6f6c
2 changed files with 29 additions and 19 deletions

View File

@@ -21,10 +21,12 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog import androidx.compose.ui.window.Dialog
import com.opencsv.CSVReader import com.opencsv.CSVReader
import com.opencsv.exceptions.CsvMalformedLineException
import com.yogeshpaliyal.common.data.AccountModel import com.yogeshpaliyal.common.data.AccountModel
import com.yogeshpaliyal.keypass.R import com.yogeshpaliyal.keypass.R
import com.yogeshpaliyal.keypass.ui.redux.actions.Action import com.yogeshpaliyal.keypass.ui.redux.actions.Action
import com.yogeshpaliyal.keypass.ui.redux.actions.ToastAction import com.yogeshpaliyal.keypass.ui.redux.actions.ToastAction
import java.io.FileNotFoundException
class ChromeAccountImporter : AccountsImporter { class ChromeAccountImporter : AccountsImporter {
override fun getImporterTitle(): Int = R.string.google_backup override fun getImporterTitle(): Int = R.string.google_backup
@@ -39,27 +41,33 @@ class ChromeAccountImporter : AccountsImporter {
val context = LocalContext.current val context = LocalContext.current
LaunchedEffect(key1 = file, block = { LaunchedEffect(key1 = file, block = {
val inputStream = context.contentResolver.openInputStream(file) try {
val reader = CSVReader(inputStream?.reader()) val inputStream = context.contentResolver.openInputStream(file)
val myEntries: List<Array<String>> = reader.readAll() val reader = CSVReader(inputStream?.reader())
val headers = myEntries[0] val myEntries: List<Array<String>> = reader.readAll()
val result = myEntries.drop(1).map { data -> val headers = myEntries[0]
headers.zip(data).toMap() val result = myEntries.drop(1).map { data ->
} headers.zip(data).toMap()
val listOfAccounts = ArrayList<AccountModel>() }
result.forEach { val listOfAccounts = ArrayList<AccountModel>()
listOfAccounts.add( result.forEach {
AccountModel( listOfAccounts.add(
title = it["name"], AccountModel(
notes = it["note"], title = it["name"],
password = it["password"], notes = it["note"],
username = it["username"], password = it["password"],
site = it["url"] username = it["username"],
site = it["url"]
)
) )
) }
resolve(listOfAccounts)
onCompleteOrCancel(ToastAction(R.string.backup_restored))
} catch (e: CsvMalformedLineException) {
onCompleteOrCancel(ToastAction(R.string.invalid_csv_file))
} catch (e: FileNotFoundException) {
onCompleteOrCancel(ToastAction(R.string.file_not_found))
} }
resolve(listOfAccounts)
onCompleteOrCancel(ToastAction(R.string.backup_restored))
}) })
LoadingDialog() LoadingDialog()

View File

@@ -111,5 +111,7 @@
<string name="keepass_backup_desc">Choose KeePass CSV file to import</string> <string name="keepass_backup_desc">Choose KeePass CSV file to import</string>
<string name="generate_qr_code">Export</string> <string name="generate_qr_code">Export</string>
<string name="invalid_secret_key">Invalid secret key format</string> <string name="invalid_secret_key">Invalid secret key format</string>
<string name="invalid_csv_file">Invalid CSV File</string>
<string name="file_not_found">File not found (please try again)</string>
</resources> </resources>