Prevent InvalidStateException when response has already ended. #951
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR prevents the scenario described on the stacktrace below. It happens when a prior handler than the template handler, breaks the handler chain while performing some validation. This change first checks whether the response has already ended before trying to write the template result to the response.
Jun 18, 2018 8:34:57 PM io.vertx.ext.web.impl.RoutingContextImplBase
SEVERE: Unexpected exception in route
java.lang.IllegalStateException: Response has already been written
at io.vertx.core.http.impl.HttpServerResponseImpl.checkValid(HttpServerResponseImpl.java:545)
at io.vertx.core.http.impl.HttpServerResponseImpl.putHeader(HttpServerResponseImpl.java:189)
at io.vertx.ext.web.handler.impl.TemplateHandlerImpl.lambda$handle$0(TemplateHandlerImpl.java:50)
at io.vertx.ext.web.templ.impl.PebbleTemplateEngineImpl.render(PebbleTemplateEngineImpl.java:89)
at io.vertx.ext.web.handler.impl.TemplateHandlerImpl.handle(TemplateHandlerImpl.java:48)
at io.vertx.ext.web.handler.impl.TemplateHandlerImpl.handle(TemplateHandlerImpl.java:28)
at io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:219)
at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:120)
at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:133)
at com.fegsoft.pricinglist.AppServerVerticle.lambda$5(AppServerVerticle.java:128)
at io.vertx.core.eventbus.impl.EventBusImpl.lambda$convertHandler$1(EventBusImpl.java:349)
at io.vertx.core.eventbus.impl.HandlerRegistration.deliver(HandlerRegistration.java:223)
at io.vertx.core.eventbus.impl.HandlerRegistration.handle(HandlerRegistration.java:200)
at io.vertx.core.eventbus.impl.EventBusImpl.lambda$deliverToHandler$3(EventBusImpl.java:533)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:339)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:886)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:745)