zeroc-ice / ice

All-in-one solution for creating networked applications with RPC, pub/sub, server deployment, and more.
https://zeroc.com
GNU General Public License v2.0
2k stars 592 forks source link

Add new ErrorObserverMiddleware in Java #2299

Closed bernardnormier closed 2 weeks ago

bernardnormier commented 2 weeks ago

This PR adds a new error observer middleware in Java.

This middleware allows the application to observe java.lang.Error thrown by the dispatch. It receives errors thrown synchronously and errors reported through the CompletionStage<OutgoingResponse> returned by the dispatch. In this latter case, the error can be reported by a call in the dispatch thread or in another thread.

The error observer callback (Java Consumer<java.lang.Error>) typically logs the error or alert the user in some way. I chose the term "observer" because it's not expected to "handle" the error. Nevertheless, if this callback throws an exception while observing an error, this exception becomes the new outcome of the dispatch.

A limitation of this middleware is it doesn't see and therefore can't observe errors thrown/reported higher-up in the dispatch, namely by ConnectionI/CollocatedRequestHandler and the two built-in internal middleware (Logger and the DispatchObserver middleware used for Metrics). It's fortunately unlikely these higher-up elements throw any error - they don't do that much.

Finally, I updated the logic in ConnectionI/CollocatedRequestHandler to rethrow any error. All errors and RuntimeException thrown synchronously by the dispatch are now handled. No error is let through to the ThreadPool code.

Fixes #2276.