From 047b3454af13f02126635c3fc01e6699c596ad22 Mon Sep 17 00:00:00 2001 From: Artem Chepurnoy Date: Tue, 16 Jan 2024 10:34:00 +0200 Subject: [PATCH] A better Failed to decode a cipher error message --- .../provider/bitwarden/crypto/BitwardenCrypto.kt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/crypto/BitwardenCrypto.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/crypto/BitwardenCrypto.kt index 006f6bd4..b4092f8a 100644 --- a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/crypto/BitwardenCrypto.kt +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/crypto/BitwardenCrypto.kt @@ -182,11 +182,19 @@ private fun Decoder.withExceptionHandling( val result = kotlin.runCatching { this(cipherText) }.getOrElse { e -> + val type = cipherText.substringBefore('.') + // If the cipher text for some reason doesn't have a + // dot separated type, then take only first N symbols + // to avoid showing the whole cipher text in the error + // message. + .take(8) val info = listOfNotNull( symmetricCryptoKey?.let { "symmetric key is ${it.data.size}b long" }, asymmetricCryptoKey?.let { "asymmetric key is ${it.privateKey.size}b long" }, ).joinToString() - val msg = "Failed to decode a cipher-text: $key, $info" + val cause = e.localizedMessage ?: e.message + val msg = "Failed to decode a cipher-text with the type '$type': $key, $info. " + + "$cause" throw DecodeException(msg, e) } result