ResponseEntity created from Source<ByteString,M> - how to react to (exceptional) completion of this stream?

Hi,

I am completing one of my routes with an entity created from a Source<ByteString,M> as follows:

HttpResponse.create().withEntity(HttpEntities.create(TEXT_CSV_UTF8, mysource));

Since this source does I/O, it may fail both during materialization and while running. I haven’t observed the latter yet, but a failure during materialization:

  • makes the server close the connection after the headers have been sent to the client - this is the behaviour I would expect
  • does not log anything as far as I can see - this seems slightly unexpected.

Most importantly, all result transformation directives and exception handlers have already been applied at this point, so I have no natural point where I could catch this problem and do some server-side reporting/recovery. (It’s clear to me that I cannot do anything to help the client at this point, the headers are already sent so the damage is done.)

Is there a way to get something, for example a CompletableFuture, from the API, that would allow me to check whether mysource and the entire “response body builder graph” have completed normally or exceptionally?

You can use watchTermination operator on mysource to get a CompletableFuture that will be completed when the stream completes with success or with failure. You can get access to that future in the callback to mapMaterializeValue.

Thank you very much. I was aware of watchTermination but forgot that I could access it via mapMaterializedValue even if the materialization of the graph happens outside of my code’s control.