How do I handle streaming data in tornado asynchronously, while handling the response code synchronously?
<p>My tornado API call will be calling another URL, then streaming the result back to the client. However, if the internal URL returns an error code, I want to handle my own error separately, and stream the error content to the client. What I currently have is this:</p>

<pre><code>@web.asynchronous
@gen.coroutine
def get(self, job_id):
    url = ...
    client = httpclient.AsyncHTTPClient()

    # handle_chunk will forward received bytes to the client, allowing
    # other HTTP requests to be handled concurrently
    response = yield client.fetch(httpclient.HTTPRequest(
        url=url,
        streaming_callback=self._handle_chunk))
    self.finish()

def _handle_chunk(self, chunk):
    self.write(chunk)
    self.flush()
</code></pre>

<p>I need to modify this to only start forwarding chunks if the response code is in the 200 family, but the response isn't yielded by <code>client.fetch</code> until the whole request is complete. Any idea how to do this?</p>