Better error management

This commit is contained in:
J-Jamet
2022-05-18 18:35:24 +02:00
parent 259c8a4bd9
commit 344118a755
3 changed files with 56 additions and 9 deletions

View File

@@ -24,21 +24,60 @@ import androidx.annotation.StringRes
import com.kunzisoft.keepass.R import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.node.NodeId import com.kunzisoft.keepass.database.element.node.NodeId
import com.kunzisoft.keepass.database.element.node.Type import com.kunzisoft.keepass.database.element.node.Type
import java.io.PrintStream
import java.io.PrintWriter
import java.lang.StringBuilder
abstract class DatabaseException : Exception { abstract class DatabaseException : Exception {
var innerMessage: String? = null
abstract var errorId: Int abstract var errorId: Int
var parameters: (Array<out String>)? = null var parameters: (Array<out String>)? = null
var mThrowable: Throwable? = null
constructor() : super() constructor() : super()
constructor(message: String) : super(message) constructor(message: String) : super(message)
constructor(message: String, throwable: Throwable) : super(message, throwable) constructor(message: String, throwable: Throwable) {
constructor(throwable: Throwable) : super(throwable) mThrowable = throwable
innerMessage = StringBuilder().apply {
append(message)
if (throwable.localizedMessage != null) {
append(" ")
append(throwable.localizedMessage)
}
}.toString()
}
constructor(throwable: Throwable) {
mThrowable = throwable
innerMessage = throwable.localizedMessage
}
fun getLocalizedMessage(resources: Resources): String { fun getLocalizedMessage(resources: Resources): String {
parameters?.let { val localMessage = parameters?.let {
return resources.getString(errorId, *it) resources.getString(errorId, *it)
} ?: return resources.getString(errorId) } ?: resources.getString(errorId)
return StringBuilder().apply {
append(localMessage)
if (innerMessage != null) {
append(" ")
append(innerMessage)
}
}.toString()
}
override fun printStackTrace() {
mThrowable?.printStackTrace()
super.printStackTrace()
}
override fun printStackTrace(s: PrintStream) {
mThrowable?.printStackTrace(s)
super.printStackTrace(s)
}
override fun printStackTrace(s: PrintWriter) {
mThrowable?.printStackTrace(s)
super.printStackTrace(s)
} }
} }

View File

@@ -125,7 +125,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
fun addRequestChallengeListener(requestChallengeListener: RequestChallengeListener) { fun addRequestChallengeListener(requestChallengeListener: RequestChallengeListener) {
mainScope.launch { mainScope.launch {
if (!mRequestChallengeListenerChannel.isEmpty) { if (!mRequestChallengeListenerChannel.isEmpty) {
mRequestChallengeListenerChannel.cancel(CancellationException("Challenge already requested")) mRequestChallengeListenerChannel.cancel(CancellationException(getString(R.string.error_challenge_already_requested)))
mRequestChallengeListenerChannel = Channel(0) mRequestChallengeListenerChannel = Channel(0)
} else { } else {
mRequestChallengeListenerChannel.send(requestChallengeListener) mRequestChallengeListenerChannel.send(requestChallengeListener)
@@ -234,11 +234,16 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
fun respondToChallenge(response: ByteArray) { fun respondToChallenge(response: ByteArray) {
mainScope.launch { mainScope.launch {
if (response.isEmpty()) { if (!mResponseChallengeChannel.isEmpty) {
mResponseChallengeChannel.cancel(CancellationException(("Unable to get the response from challenge"))) mResponseChallengeChannel.cancel(CancellationException(getString(R.string.error_response_already_provided)))
mResponseChallengeChannel = Channel(0) mResponseChallengeChannel = Channel(0)
} else { } else {
mResponseChallengeChannel.send(response) if (response.isEmpty()) {
mResponseChallengeChannel.cancel(CancellationException(getString(R.string.error_no_response_from_challenge)))
mResponseChallengeChannel = Channel(0)
} else {
mResponseChallengeChannel.send(response)
}
} }
} }
} }

View File

@@ -195,6 +195,9 @@
<string name="error_duplicate_file">The file data already exists.</string> <string name="error_duplicate_file">The file data already exists.</string>
<string name="error_remove_file">An error occurred while removing the file data.</string> <string name="error_remove_file">An error occurred while removing the file data.</string>
<string name="error_start_database_action">An error occurred while performing an action on the database.</string> <string name="error_start_database_action">An error occurred while performing an action on the database.</string>
<string name="error_challenge_already_requested">"Challenge already requested"</string>
<string name="error_response_already_provided">Response already provided.</string>
<string name="error_no_response_from_challenge">Unable to get the response from the challenge.</string>
<string name="field_name">Field name</string> <string name="field_name">Field name</string>
<string name="field_value">Field value</string> <string name="field_value">Field value</string>
<string name="file_not_found_content">Could not find file. Try reopening it from your file browser.</string> <string name="file_not_found_content">Could not find file. Try reopening it from your file browser.</string>