title: Haskell wai middleware - how to run application if 404 is returned from the first application?
body: <p>I'm trying to 'join' two wai <code>Application</code>s together. Essentially the first app will either serve a <strong>404 Not found response</strong> or a response that is not a <strong>404 Not found response</strong>. I'd like to add a second application that will attempt to process the request if the first case (first application returns a <strong>404</strong>) is present.</p>
  <p>However looking at the type of <code>Application</code>:</p>
  <pre><code>type Application = Request -&gt; (Response -&gt; IO ResponseReceived) -&gt; IO ResponseReceived
  </code></pre>
  <p>It's not clear how to inspect the response? As wouldn't I just end up with <code>ResponseRecived</code> as the end result - which does not seem to contain any response code. As seen below:</p>
  <pre><code>xyzMiddle :: (Request -&gt; (Response -&gt; IO ResponseReceived) -&gt; IO ResponseReceived) -&gt;
               (Request -&gt; (Response -&gt; IO ResponseReceived) -&gt; IO ResponseReceived)
  xyzMiddle app req respond = do
    zzz &lt;- (app req respond)
    pure zzz
  </code></pre>
  <p>How could I implement this? And or, what am I failing to understand in the above?</p>