Hello,
We have a custom error handler that collects extra information and sends it as a response using the context.render method.
The problem is, that if the incoming request is larger than the configured limit, the NettyHandler automatically sends an error with the 413 status code.
|
context.sendError(new StatusCodeException(StatusCode.REQUEST_ENTITY_TOO_LARGE)); |
However - since we are using the render method - our error handler cannot run successfully because the context object itself is not complete at this stage. In our concrete example, it cannot determine the encoder to use because the route member is null in the context.
There are two different solutions that came to my mind:
Possible solution 1
Instead of using the render method, we could use the registered encoders to manually encode the content then simply send the response. However, I don't know any way to obtain an encoder, except for creating it from scratch, which is inconvenient. We can register encoders, but their usage seems to be private.
So something like app.encoderOf(mediaType) would be nice.
Possible solution 2
Somehow change the order of things in the NettyHandler and initialze the context when the first part of a request is received. In this case if there are any errors while the request is processed, the error handler could safely use the render method.
Hello,
We have a custom error handler that collects extra information and sends it as a response using the
context.rendermethod.The problem is, that if the incoming request is larger than the configured limit, the
NettyHandlerautomatically sends an error with the 413 status code.jooby/modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyHandler.java
Line 114 in d61c655
However - since we are using the
rendermethod - our error handler cannot run successfully because thecontextobject itself is not complete at this stage. In our concrete example, it cannot determine the encoder to use because theroutemember is null in thecontext.There are two different solutions that came to my mind:
Possible solution 1
Instead of using the render method, we could use the registered encoders to manually encode the content then simply
sendthe response. However, I don't know any way to obtain an encoder, except for creating it from scratch, which is inconvenient. We can register encoders, but their usage seems to be private.So something like
app.encoderOf(mediaType)would be nice.Possible solution 2
Somehow change the order of things in the
NettyHandlerand initialze the context when the first part of a request is received. In this case if there are any errors while the request is processed, the error handler could safely use therendermethod.