How to access HTTP error causes from Netty channel handler #9168
Replies: 2 comments 2 replies
-
FWIW, if anyone else wants to know, an inelegant solution is to create an |
Beta Was this translation helpful? Give feedback.
-
It's not really possible in a reliable way, especially with Micronaut HTTP 4. In Micronaut HTTP 3, the Micronaut HttpRequest/Response objects are usually sent on the pipeline, between the streams handler and the routinginboundhandler. At this point you could access the exception. However at this point, you have a Micronaut request/response which you might as well access using a normal filter, so this doesn't really help. Beyond the streams handler (which transforms netty request/response to/from micronaut request/response) you have the problem you describe. In Micronaut HTTP 4, the streams handler is not used anymore, so the Micronaut request/response is never on the pipeline. There's no way to get at the exception then. What you could do is in a filter set an attribute on the netty channel with any metadata you want. But this is unreliable for HTTP/1 because of pipelining: A filter may see responses in a different order than they are actually sent back to the client, so it's possible you could mix up this metadata. For HTTP/2 however it should be safe (for Micronaut 4 at least) because we use a new channel for every stream. |
Beta Was this translation helpful? Give feedback.
-
Hi there,
I am writing an HTTP API using Micronaut 3.8.6. I am attempting to implement functionality that logs information to console that is later consumed (by an external process) for monitoring purposes via Splunk, Kibana etc.
The information that is logged is a summary of an inbound request, and the corresponding outbound response. This includes:
As I require access to the request body, I have found that the above can't be achieved using an
HttpServerFilter
, per #7986.As such, I have implemented a
ChannelDuplexHandler
that reads in the request information (including request body) inchannelRead()
, and similarly the response information inwrite()
. I took inspiration from Micronaut'sHttpAccessLogHandler
(https://github.com/micronaut-projects/micronaut-core/blob/v3.8.6/http-server-netty/src/main/java/io/micronaut/http/server/netty/handler/accesslog/HttpAccessLogHandler.java).However, when an error response is being returned (i.e. HTTP >= 400), I can't for the life of me figure out how to get access to the underlying cause?! I can see that the
RouteExecutor
is adding underlying exceptions to theMutableHttpResponse
(i.e.response.setAttribute(HttpAttributes.EXCEPTION, cause)
https://github.com/micronaut-projects/micronaut-core/blob/v3.8.6/http-server/src/main/java/io/micronaut/http/server/RouteExecutor.java#L258), but have no idea how to get at it (and hence the response attributes) from a channel handler?Is there a way to intercept
io.micronaut.http.MutableHttpResponse
orio.micronaut.http.HttpResponse
in channel handlers? Or is there some other way I can get at error causes?Thanks
Beta Was this translation helpful? Give feedback.
All reactions