Hello, in case the serializer throws for various r...
# serialization
g
Hello, in case the serializer throws for various reasons, which is the optimal way to handle the throwing and return a bad response? im using this but i do not like it much because i try-catch. im looking for a way to avoid that.
Copy code
fun <T> getOrNull(block: () -> T): T? = runCatching {
    block()
}.getOrNull()

private inline fun <reified T> deserializeMessage(message: T): IncomingMessage? {
    return getOrNull { deserialize(message) } as IncomingMessage?
}
n
If the serializer throws an exception, your only two choices are to catch that exception somewhere or let it crash. So there's no way to avoid catching if you want a failure to be null.
🙏 1