Skip to content

Commit

Permalink
chore: vc-revocation cli - hint on connect-exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeplotean committed Jun 28, 2023
1 parent 51c9fa6 commit 2d90242
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/main/kotlin/id/walt/cli/VcCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ import id.walt.signatory.Signatory
import id.walt.signatory.dataproviders.CLIDataProvider
import id.walt.signatory.revocation.RevocationClientService
import io.ktor.util.date.*
import io.ktor.util.reflect.*
import mu.KotlinLogging
import java.io.File
import java.io.StringReader
import java.net.ConnectException
import java.nio.file.Path
import java.sql.Timestamp
import java.time.LocalDateTime
Expand Down Expand Up @@ -558,9 +560,13 @@ class VcRevocationCheckCommand : CliktCommand(
val vcFile: File by argument().file()
override fun run() = vcFile.takeIf { it.exists() }?.run {
println("Checking revocation status for credential stored at: ${vcFile.absolutePath}")
val status = RevocationClientService.check(this.readText().toVerifiableCredential())
println("Revocation status:")
println(Klaxon().toJsonString(status).prettyPrint())
runWithErrorHandling(
runner = { RevocationClientService.check(this.readText().toVerifiableCredential()) },
onSuccess = {
println("Revocation status:")
println(Klaxon().toJsonString(it).prettyPrint())
}
)
} ?: Unit
}

Expand All @@ -570,9 +576,31 @@ class VcRevocationRevokeCommand: CliktCommand(
val vcFile: File by argument().file()
override fun run() = vcFile.takeIf { it.exists() }?.run {
println("Revoking credential stored at: ${vcFile.absolutePath}")
val result = RevocationClientService.revoke(this.readText().toVerifiableCredential())
println("Revocation result:")
println(Klaxon().toJsonString(result).prettyPrint())
runWithErrorHandling(
runner = { RevocationClientService.revoke(this.readText().toVerifiableCredential()) },
onSuccess = {
println("Revocation result:")
println(Klaxon().toJsonString(it).prettyPrint())
}
)
} ?: Unit
}

internal fun <T> runWithErrorHandling(
runner: () -> T,
onSuccess: ((T) -> Unit)? = null,
onFailure: ((Throwable) -> Unit)? = null
) {
runCatching {
runner()
}.onSuccess {
onSuccess?.invoke(it)
}.onFailure {
println(it.localizedMessage)
if (it.instanceOf(ConnectException::class)) {
println("Looks like couldn't reach the Signatory API. Make sure to run \"ssikit serve\" first.")
}
onFailure?.invoke(it)
}
}
//endregion

0 comments on commit 2d90242

Please sign in to comment.