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.window.Dialog
import com.opencsv.CSVReader
import com.opencsv.exceptions.CsvMalformedLineException
import com.yogeshpaliyal.common.data.AccountModel
import com.yogeshpaliyal.keypass.R
import com.yogeshpaliyal.keypass.ui.redux.actions.Action
import com.yogeshpaliyal.keypass.ui.redux.actions.ToastAction
import java.io.FileNotFoundException
class ChromeAccountImporter : AccountsImporter {
override fun getImporterTitle(): Int = R.string.google_backup
@@ -39,27 +41,33 @@ class ChromeAccountImporter : AccountsImporter {
val context = LocalContext.current
LaunchedEffect(key1 = file, block = {
val inputStream = context.contentResolver.openInputStream(file)
val reader = CSVReader(inputStream?.reader())
val myEntries: List<Array<String>> = reader.readAll()
val headers = myEntries[0]
val result = myEntries.drop(1).map { data ->
headers.zip(data).toMap()
}
val listOfAccounts = ArrayList<AccountModel>()
result.forEach {
listOfAccounts.add(
AccountModel(
title = it["name"],
notes = it["note"],
password = it["password"],
username = it["username"],
site = it["url"]
try {
val inputStream = context.contentResolver.openInputStream(file)
val reader = CSVReader(inputStream?.reader())
val myEntries: List<Array<String>> = reader.readAll()
val headers = myEntries[0]
val result = myEntries.drop(1).map { data ->
headers.zip(data).toMap()
}
val listOfAccounts = ArrayList<AccountModel>()
result.forEach {
listOfAccounts.add(
AccountModel(
title = it["name"],
notes = it["note"],
password = it["password"],
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()

View File

@@ -111,5 +111,7 @@
<string name="keepass_backup_desc">Choose KeePass CSV file to import</string>
<string name="generate_qr_code">Export</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>