A better Failed to decode a cipher error message

This commit is contained in:
Artem Chepurnoy 2024-01-16 10:34:00 +02:00
parent 46478e9052
commit 047b3454af
No known key found for this signature in database
GPG Key ID: FAC37D0CF674043E
1 changed files with 9 additions and 1 deletions

View File

@ -182,11 +182,19 @@ private fun Decoder.withExceptionHandling(
val result = kotlin.runCatching { val result = kotlin.runCatching {
this(cipherText) this(cipherText)
}.getOrElse { e -> }.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( val info = listOfNotNull(
symmetricCryptoKey?.let { "symmetric key is ${it.data.size}b long" }, symmetricCryptoKey?.let { "symmetric key is ${it.data.size}b long" },
asymmetricCryptoKey?.let { "asymmetric key is ${it.privateKey.size}b long" }, asymmetricCryptoKey?.let { "asymmetric key is ${it.privateKey.size}b long" },
).joinToString() ).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) throw DecodeException(msg, e)
} }
result result