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.
George
04/21/2022, 6:46 AM
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
nschulzke
04/21/2022, 1:45 PM
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.