Hey David!
Nice, thanks for the reply. Yeah, I'm already putting stuff into ngx.ctx to be used for building the custom body (and to signal that one of the error scenarios has occurred), so it looks like we're on the same page with that part! But my understanding is you can't mix content_by_lua and balancer_by_lua, so I wasn't able to leverage content_by_lua like I had hoped.
That said, this morning, I realized I can leverage body_filter_by_lua (at least in my balancer_by_lua case where no peer is found/set,). I just let it fall through to body_filter_by_lua (instead of exiting early), and have code check ngx.ctx for certain attributes that instruct it to set the error page accordingly.
At first it wasn't working, but then I realized it was because I wasn't sending a trailing "\n" in the body I was assigning to ngx.arg[1] -- whoops. ..and now it seems to be working!
This might not be the *most* optimal way to do it, but the change to implement it was actually pretty small, and it seems to fit in with how requests/responses flow through the phases.
If you or anyone else has any other ideas, definitely chime in (because it's fun to see what other people are doing), but, I've got something going for now. :)
Thanks!
Chet