Hi folks, I have a question as a relatively newbie...
# coroutines
s
Hi folks, I have a question as a relatively newbie in the coroutine world. I'm trying to refactor this piece of code:
Copy code
val oas = specs.map {
    async {
      try {
        client.get(it).body<OpenApiSpec>() // Ktor HTTP client
      } catch (e: ClientRequestException) {
        null
      }
    }
  }.awaitAll()
I'd like to abstract away the exception handling, so I've tried something like this:
Copy code
suspend inline fun <reified T> HttpResponse.bodyOrNull(): T? = try {
  body()
} catch (e: ClientRequestException) {
  null
}

val oas = specs.map {
  async {
    client.get(it).bodyOrNull<OpenApiSpec>()
  }
}.awaitAll()
But instead of swallowing the exceptions and returning null as in the example above, I get the exception bubbling up. I suspect I'm not yet fully understanding the exception handling model for coroutines, could anybody help shed some light? Thanks in advance. (Btw, I obtain the same result if I wrap the try/catch with
coroutineScope
)
✔️ 1
Solved the problem by myself. In case anyone will wonder or is curious, it's
get()
throwing an exception, not
body()
so it makes sense that my refactoring has no effect