-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error handling #21
Comments
Do you think, such approach is reasonable? I have no idea how does it work in JS target. My original implementation was very simple: interface TagConsumer<out R> {
// ...
// default implementation simply re-throws the exception after closing the tag
fun onError(exception: Exception) : Unit = throw exception
}
fun <T : Tag> T.visit(block: T.() -> Unit) {
consumer.onTagStart(this)
try {
this.block()
} catch (err: Exception) {
consumer.onError(err)
} finally {
// all tags are properly closed
consumer.onTagEnd(this)
}
} |
Tag Consumers might handle exceptions (issue Kotlin#21)
The only problem I can see is that if we have a chain of consumers then the error handler in the middle of the chain can only write directly to the delegate rather than pass through the whole chain pros: we can't get infinite error handling issue |
In case of exception, current implementation simply aborts the processing.
I've introduced an
onError
handler in theTagConsumer
, so that the library:As the result, it's possible to use such TagConsumer:
It cannot get much simpler than that. As you see, it has all the freedom to properly react to the exception. For example, the tag contents might simply remain empty, while the rest of the page remains fully-functional.
I use Kotlinx.html for stream-like processing: page headers are sent almost instantly (by abusing
TagConsumer.finalize
andPrintWriter.flush
), while the rest is generated gradually, like in the original PHP (but with good compiler) for lighting-fast zero-delay instant content delivery. Therefore errors are rather common and must be handled.The text was updated successfully, but these errors were encountered: