Mark Fisher
04/18/2019, 8:47 AMinterface DataService {
fun createMaybeObjects(count: Int): Maybe<List<Data>>
fun createListObjects(count: Int): List<Data>
}
@Singleton
class DefaultDataService(private val repo: DataRepository) : DataService {
override fun createMaybeObjects(count: Int): Maybe<List<Data>> {
return Maybe.fromCallable { repo.createList(count) }
}
override fun createListObjects(count: Int): List<Data> {
return Maybe.fromCallable { repo.createList(count) }.blockingGet()
}
}
@Singleton
class DataRepository {
private val mapper = jacksonObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
fun createList(num: Int): List<Data> {
val data = mapper.readValue<Data>("bad data")
return MutableList(num) { data }
}
}
data class Data(
val name: String,
val size: Int
)
the line val data = mapper.readValue<Data>("bad data") causes an exception, which is sometimes picked up by an ExceptionHandler, sometimes not.
The code for the handler is:
@Produces
@Singleton
@Requires(classes = [Exception::class, ExceptionHandler::class])
class GlobalExceptionHandler : ExceptionHandler<Exception, HttpResponse<Any>> {
override fun handle(request: HttpRequest<Any>, exception: Exception): HttpResponse<Any> {
println("handle called")
exception.printStackTrace()
return HttpResponse.serverError(0)
}
}
It's all in the repo linked with notes and a simple build.