Hi.
I'm new both to Vert.x and Kotlin, and I'm trying to figure out an optimal way to dealing with exceptions in event handlers.
I want to log exceptions happening in those handlers throughout the application, with minimal boilerplate code.
Let's say, I have this doStuff() method that fails every now and then:
class MainVerticle : CoroutineVerticle() {
override suspend fun start() {
vertx.setPeriodic(1000) {
launch {
doStuff()
}
}
}
private suspend fun doStuff() {
delay(500)
if (Random.nextInt(5) == 0) {
throw RuntimeException("Something bad just happened")
}
println("Job done")
}
}
If the method throws an exception, I need just to log this event and continue app execution. Vertx.exceptionHandler is not fired in this case.
Here I found that there's a way to use the launch() method with the custom CoroutineExceptionHandler. It works, but it looks slightly cumbersome to me - it's easy to forget to supply this handler and miss exceptions, especially when there are many launch() invocations exist.
Is there a better way to catch those exceptions globally per verticle or per Vertx instance?