eclipse-vertx / vert.x

Vert.x is a tool-kit for building reactive applications on the JVM
http://vertx.io
Other
14.32k stars 2.08k forks source link

io.vertx.ext.web.RoutingContext json() method not reporting json.EncodeException #5256

Closed pjwigan closed 3 months ago

pjwigan commented 3 months ago

Version

4.5.9

Summary

An HTTP call started giving 500 Routing failures following a change to a referenced class. A bug in that class meant a json.EncodeException is being thrown within RoutingContext json(...). However this exception is not being passed back to the caller. The RoutingContext is not ended, hence the subsequent routing failure.

Failing code

private void fetchPanels(RoutingContext ctx) {
    try {
        int n = Integer.parseInt(ctx.pathParam("n"));
        List<PointRef2> panels = selectPanelsForNetwork(n);
        ctx.json(panels);
        logger.debug("fetchPanels ended? {}", ctx.response().ended());  <-- FALSE
    } catch (Exception e) {
        ...
    }
}

Code that revealed the problem

private void fetchPanels(RoutingContext ctx) {
    try {
        int n = Integer.parseInt(ctx.pathParam("n"));
        List<PointRef2> panels = selectPanelsForNetwork(n);
        JsonArray ja = new JsonArray(panels);      <-- throws EncodeException 
                ...
    } catch (Exception e) {
        ...
    }
}

Extra

The bug in our code was a guard added to an accessor method throwing an exception, thus causing the EncodeException.

tsegismont commented 3 months ago

RoutingContext.json() returns a future that is completed when the response has been sent successfully, otherwise failed.

Besides, if there is an encoding issue, the method invokes RoutingContext.fail(). So if you install a handler for the failure (see the docs), you should get the notification too.

Closing, since this doesn't look like a bug