Mark Fisher
04/18/2019, 11:11 AMException, but instead use a custom class (e.g. ApiException) in the handler, and then add an .onErrorResumeNext{} to bubble the exception:
@Produces
@Singleton
@Requires(classes = [ApiException::class])
class ApiExceptionHandler : ExceptionHandler<ApiException, HttpResponse<Any>> {
override fun handle(request: HttpRequest<Any>, exception: ApiException): HttpResponse<Any> {
println("ApiExceptionHandler handle called")
return HttpResponse.serverError(0)
}
}
class ApiException(message: String? = null, cause: Throwable) : Exception(message, cause) {
constructor(cause: Throwable) : this(null, cause)
}
// ... in controller:
override fun createMaybeObjects(count: Int): Maybe<List<Data>> {
return Maybe.fromCallable { repo.createList(count) }
.onErrorResumeNext { t: Throwable ->
Maybe.error(ApiException(t))
}
}
which appears to be rock solid.
Interestingly, there's still a 50/50 issue if I use Maybe.error(Exception(t)) and don't use a custom exception anywhere.