Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Not sure how to respond to exceptions thrown when streaming Ratpack responses via Publisher.
import static ratpack.groovy.Groovy.ratpack
import static ratpack.http.ResponseChunks.*
import org.reactivestreams.Subscription
import ratpack.error.ServerErrorHandler
import ratpack.handling.Context
import ratpack.render.RendererSupport
ratpack {
bindings {
bind ThingRenderer
bindInstance ServerErrorHandler, { context, error ->
context.response.status 500
context.render "server error = ${error}" } as ServerErrorHandler
handlers {
path('render-error') {
context.render(new Thing())
path('render-ok') {
context.render(new Thing(message: 'hello, how are you'))
path('streamed') {
TransformablePublisher<String> publisher = { subscriber ->
subscriber.onSubscribe(new Subscription() {
void request(long n) {
subscriber.onNext('how are you')
subscriber.onError(new RuntimeException('whoops'))
void cancel() {
context.render(stringChunks('text/plain', publisher.buffer()))
class Thing {
String message;
class ThingRenderer extends RendererSupport<Thing> {
void render(Context context, Thing thing) {
if (!thing.message) {
throw new RuntimeException('thing must have message')

This comment has been minimized.

Copy link
Owner Author

brucenunk commented Jan 8, 2016

http://localhost:5050/render-error notifies the ServerErrorHandler of the exception whereas http://localhost:5050/streamed does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.