I maintain a Kotlin HTML Library, and one of the m...
# ktor
s
I maintain a Kotlin HTML Library, and one of the modules I provide involves integration with ktor. In use, I’m seeing exceptions eaten, and I think it has to do with my improper use of
OutgoingContent
. In the below linked code, the exception is happening inside of the
func
parameter after it’s been called. My guess is that at that point the return status code has already been chosen, which is too late, and why the exception handlers are ignored. https://github.com/ScottPierce/kotlin-html/blob/master/kotlin-html-ktor/src/main/kotlin/dev/scottpierce/html/ktor/RespondHtml.kt#L41-L58 The obvious solution is to write to a
StringBuilder
, then give the output
String
to the
ByteWriteChannel
, instead of writing directly to the
ByteWriteChannel
. I thought I’d ask: Is there is a better way I can handle this that I haven’t considered?
I think I got this from copying ktor’s html-builder feature. It seems like it would have the same problem - https://github.com/ktorio/ktor/blob/master/ktor-features/ktor-html-builder/jvm/src/io/ktor/html/RespondHtml.kt
c
This is quite common issue when handling errors during page generation. So there are generally two solution: generate page in memory (StringBuilder) or retrieve all data before generating page (ViewModel) and then generate over model class
👍 1