<p dir="auto">at async Object.respondWith (deno:ext/http/01_http.js:211:11)</p>
<p dir="auto">我用的deno+oak都是最新版写的接口，调用接口时有几率（特殊环境比如用rax写微信小程序里面usePageShow才出现这种问题，postman等单独调用接口几乎没有失败过）报错这个。</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="deno --version
deno 1.17.1 (release, x86_64-apple-darwin)
v8 9.7.106.15
typescript 4.5.2"><pre class="notranslate"><code class="notranslate">deno --version
deno 1.17.1 (release, x86_64-apple-darwin)
v8 9.7.106.15
typescript 4.5.2
</code></pre></div>
<p dir="auto">If I reload the browser before the response from the server arrives I get the error too, how can I catch this error so that the application does not crash?</p>
<p dir="auto">Example:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const sleep = (ms: number) =&gt; {
  return new Promise(resolve =&gt; setTimeout(resolve, ms))
}

const server = Deno.listen({ port: 8000 });

console.log(&quot;http://localhost:8000/&quot;);

try {
  for await (const conn of server) {
    (async () =&gt; {
      const httpConn = Deno.serveHttp(conn);
      for await (const { respondWith } of httpConn) {
        // Placeholder for some calculations or requests from other services
        // This makes the difference
        await sleep(200);
        respondWith(new Response(&quot;hello world&quot;, {
          status: 200,
        }));
      }
    })();
  }
} catch (error) {
  // Note: This is not called for the error: &quot;Http: connection closed before message completed&quot;
  console.warn(&quot;Error&quot;, error);
}"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-en">sleep</span> <span class="pl-c1">=</span> <span class="pl-kos">(</span><span class="pl-s1">ms</span>: <span class="pl-smi">number</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">return</span> <span class="pl-k">new</span> <span class="pl-smi">Promise</span><span class="pl-kos">(</span><span class="pl-s1">resolve</span> <span class="pl-c1">=&gt;</span> <span class="pl-en">setTimeout</span><span class="pl-kos">(</span><span class="pl-s1">resolve</span><span class="pl-kos">,</span> <span class="pl-s1">ms</span><span class="pl-kos">)</span><span class="pl-kos">)</span>
<span class="pl-kos">}</span>

<span class="pl-k">const</span> <span class="pl-s1">server</span> <span class="pl-c1">=</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">listen</span><span class="pl-kos">(</span><span class="pl-kos">{</span> <span class="pl-c1">port</span>: <span class="pl-c1">8000</span> <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-s">"http://localhost:8000/"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-k">try</span> <span class="pl-kos">{</span>
  <span class="pl-k">for</span> <span class="pl-k">await</span> <span class="pl-kos">(</span><span class="pl-k">const</span> <span class="pl-s1">conn</span> <span class="pl-k">of</span> <span class="pl-s1">server</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-kos">(</span><span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
      <span class="pl-k">const</span> <span class="pl-s1">httpConn</span> <span class="pl-c1">=</span> <span class="pl-smi">Deno</span><span class="pl-kos">.</span><span class="pl-en">serveHttp</span><span class="pl-kos">(</span><span class="pl-s1">conn</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
      <span class="pl-k">for</span> <span class="pl-k">await</span> <span class="pl-kos">(</span><span class="pl-k">const</span> <span class="pl-kos">{</span> respondWith <span class="pl-kos">}</span> <span class="pl-k">of</span> <span class="pl-s1">httpConn</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-c">// Placeholder for some calculations or requests from other services</span>
        <span class="pl-c">// This makes the difference</span>
        <span class="pl-k">await</span> <span class="pl-en">sleep</span><span class="pl-kos">(</span><span class="pl-c1">200</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
        <span class="pl-en">respondWith</span><span class="pl-kos">(</span><span class="pl-k">new</span> <span class="pl-smi">Response</span><span class="pl-kos">(</span><span class="pl-s">"hello world"</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
          <span class="pl-c1">status</span>: <span class="pl-c1">200</span><span class="pl-kos">,</span>
        <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
      <span class="pl-kos">}</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span> <span class="pl-k">catch</span> <span class="pl-kos">(</span><span class="pl-s1">error</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-c">// Note: This is not called for the error: "Http: connection closed before message completed"</span>
  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">warn</span><span class="pl-kos">(</span><span class="pl-s">"Error"</span><span class="pl-kos">,</span> <span class="pl-s1">error</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Start this with <code class="notranslate">deno run --allow-net app.ts</code></p>
<p dir="auto">If I remove <code class="notranslate">await sleep(200);</code> it also happens when I hold down the F5 key of the browser so that many requests are generated in a short time.</p>
<p dir="auto"><em>Originally posted by <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/JumpLink/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/JumpLink">@JumpLink</a> in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="962217456" data-permission-text="Title is private" data-url="https://github.com/denoland/deno/issues/11595" data-hovercard-type="issue" data-hovercard-url="/denoland/deno/issues/11595/hovercard?comment_id=991943630&amp;comment_type=issue_comment" href="https://github.com/denoland/deno/issues/11595#issuecomment-991943630">#11595 (comment)</a></em></p>