{"title":"Getting Serverless Errors Out Before The Function Freezes","html_url":"https://deallocatedobjects.com/posts/getting-serverless-errors-out-before-the-function-freezes","source_url":"","markdown_content":"A function on a serverless host can be frozen the moment it returns its response. If your error reporter was going to send the exception a second later, that second never comes. The user sees a 500, and you see nothing, which is the worst combination.\n\nI ran into this wiring up the Node SDK for [Runheed](https://runheed.com/?ref=deallocated-post). The SDK batches events for up to 5 seconds before sending them. That's fine on a server that stays up. On a function it means the batch can still be sitting in memory when the host puts the process to sleep.\n\n### The Failing Version\n\nThis looks right and loses errors:\n\n```typescript\nimport runheed from 'runheed';\n\nrunheed.init({ dsn: process.env.RUNHEED_DSN! });\n\nexport default {\n  async fetch(request: Request): Promise<Response> {\n    try {\n      const { cartId } = await request.json();\n      return Response.json({ ok: true, cartId });\n    } catch (err) {\n      runheed.captureException(err as Error);\n      return Response.json({ error: 'Internal error' }, { status: 500 });\n    }\n  },\n};\n```\n\n`captureException` queues the event. The function returns, the host freezes it, and the queue waits for a wake-up that may not come before the instance is thrown away.\n\n### Flush Before You're Frozen\n\nThe reporter has a `flush()` that sends whatever is queued and returns a promise. You could `await` it before returning the response, but then every failing request waits on a network call to your error tracker. Vercel has a better hook for this: `waitUntil` from `@vercel/functions` tells the platform to keep the function alive until a promise settles, after the response has gone out.\n\n```typescript\nimport runheed from 'runheed';\nimport { waitUntil } from '@vercel/functions';\n\nrunheed.init({\n  dsn: process.env.RUNHEED_DSN!,\n  environment: process.env.VERCEL_ENV ?? 'development',\n  release: process.env.VERCEL_GIT_COMMIT_SHA,\n});\n\nexport default {\n  async fetch(request: Request): Promise<Response> {\n    try {\n      const { cartId } = await request.json();\n      return Response.json({ ok: true, cartId });\n    } catch (err) {\n      runheed.captureException(err as Error, { method: request.method, url: request.url });\n      waitUntil(runheed.flush());\n      return Response.json({ error: 'Internal error' }, { status: 500 });\n    }\n  },\n};\n```\n\nThe user gets their response right away, and the function stays up just long enough to send the error.\n\nIn a Next.js 15.1 or later route handler, `after()` from `next/server` does the same job, and it's what Vercel recommends there:\n\n```typescript\nimport { after } from 'next/server';\nimport runheed from 'runheed';\n\nexport async function POST(request: Request) {\n  try {\n    // ...your code\n    return Response.json({ ok: true });\n  } catch (err) {\n    runheed.captureException(err as Error, { route: '/api/checkout' });\n    after(() => runheed.flush());\n    return Response.json({ error: 'Internal error' }, { status: 500 });\n  }\n}\n```\n\n### Why The Release Line Is There\n\nVercel sets `VERCEL_GIT_COMMIT_SHA` on every deploy, and passing it as `release` means each error is stamped with the commit that was live when it happened. That matters once your coding agent is reading the errors. It can see which deploy introduced one, fix it, and resolve it with the commit hash of the fix. If the same error comes back, it reopens with the note from that fix attached.\n\n### Check It\n\nThe way I test a setup like this is to throw on purpose from a deployed preview, return the 500, and look for the event a moment later. If it never shows up, the flush isn't running before the freeze. Runheed's `runheed_send_test_event` tool sends a sample event from your agent, which is handy for checking the DSN and project, but it won't tell you about the freeze. Only a real failure from the deployed function does.\n\nAny host that stops a process after it responds has the same problem, so check what yours offers for finishing work after the response. The Vercel setup is written up with the other frameworks in [Runheed's docs](https://runheed.com/for/vercel?ref=deallocated-post).","html_content":"<p>A function on a serverless host can be frozen the moment it returns its response. If your error reporter was going to send the exception a second later, that second never comes. The user sees a 500, and you see nothing, which is the worst combination.</p>\n<p>I ran into this wiring up the Node SDK for <a href=\"https://runheed.com/?ref=deallocated-post\">Runheed</a>. The SDK batches events for up to 5 seconds before sending them. That’s fine on a server that stays up. On a function it means the batch can still be sitting in memory when the host puts the process to sleep.</p>\n<h2 id=\"the-failing-version\" data-level=\"3\">The Failing Version</h2>\n<p>This looks right and loses errors:</p>\n<figure class=\"code\" data-lang=\"typescript\"><figcaption><span class=\"lang\">TypeScript</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> runheed </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> 'runheed'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">runheed.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">init</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">({ dsn: process.env.</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">RUNHEED_DSN</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">!</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> });</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">export</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> default</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">  async</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> fetch</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"--shiki-light:#E36209;--shiki-dark:#FFAB70\">request</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">:</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> Request</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">)</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">:</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> Promise</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">&#x3C;</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">Response</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">> {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    try</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">      const</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> { </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">cartId</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> } </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> await</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> request.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">json</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">();</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">      return</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> Response.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">json</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">({ ok: </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">true</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">, cartId });</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    } </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">catch</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> (err) {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">      runheed.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">captureException</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(err </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">as</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> Error</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">);</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">      return</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> Response.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">json</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">({ error: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">'Internal error'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> }, { status: </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">500</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> });</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    }</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">  },</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">};</span></span></code></pre></figure><p><code>captureException</code> queues the event. The function returns, the host freezes it, and the queue waits for a wake-up that may not come before the instance is thrown away.</p>\n<h2 id=\"flush-before-youre-frozen\" data-level=\"3\">Flush Before You’re Frozen</h2>\n<p>The reporter has a <code>flush()</code> that sends whatever is queued and returns a promise. You could <code>await</code> it before returning the response, but then every failing request waits on a network call to your error tracker. Vercel has a better hook for this: <code>waitUntil</code> from <code>@vercel/functions</code> tells the platform to keep the function alive until a promise settles, after the response has gone out.</p>\n<figure class=\"code\" data-lang=\"typescript\"><figcaption><span class=\"lang\">TypeScript</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> runheed </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> 'runheed'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> { waitUntil } </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> '@vercel/functions'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">runheed.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">init</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">({</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">  dsn: process.env.</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">RUNHEED_DSN</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">!</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">  environment: process.env.</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">VERCEL_ENV</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> ??</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> 'development'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">  release: process.env.</span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">VERCEL_GIT_COMMIT_SHA</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">,</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">});</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">export</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> default</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">  async</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> fetch</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"--shiki-light:#E36209;--shiki-dark:#FFAB70\">request</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">:</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> Request</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">)</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">:</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> Promise</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">&#x3C;</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">Response</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">> {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    try</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">      const</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> { </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">cartId</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> } </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> await</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> request.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">json</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">();</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">      return</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> Response.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">json</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">({ ok: </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">true</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">, cartId });</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    } </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">catch</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> (err) {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">      runheed.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">captureException</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(err </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">as</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> Error</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">, { method: request.method, url: request.url });</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">      waitUntil</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(runheed.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">flush</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">());</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">      return</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> Response.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">json</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">({ error: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">'Internal error'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> }, { status: </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">500</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> });</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    }</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">  },</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">};</span></span></code></pre></figure><p>The user gets their response right away, and the function stays up just long enough to send the error.</p>\n<p>In a Next.js 15.1 or later route handler, <code>after()</code> from <code>next/server</code> does the same job, and it’s what Vercel recommends there:</p>\n<figure class=\"code\" data-lang=\"typescript\"><figcaption><span class=\"lang\">TypeScript</span></figcaption><pre class=\"shiki shiki-themes github-light github-dark\" style=\"--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> { after } </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> 'next/server'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">import</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> runheed </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">from</span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\"> 'runheed'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">;</span></span>\n<span class=\"line\"></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">export</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> async</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\"> function</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> POST</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(</span><span style=\"--shiki-light:#E36209;--shiki-dark:#FFAB70\">request</span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">:</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> Request</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">) {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">  try</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#6A737D;--shiki-dark:#6A737D\">    // ...your code</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> Response.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">json</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">({ ok: </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">true</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> });</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">  } </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">catch</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> (err) {</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">    runheed.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">captureException</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(err </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">as</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\"> Error</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">, { route: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">'/api/checkout'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> });</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">    after</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">(() </span><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">=></span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> runheed.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">flush</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">());</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#D73A49;--shiki-dark:#F97583\">    return</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> Response.</span><span style=\"--shiki-light:#6F42C1;--shiki-dark:#B392F0\">json</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">({ error: </span><span style=\"--shiki-light:#032F62;--shiki-dark:#9ECBFF\">'Internal error'</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> }, { status: </span><span style=\"--shiki-light:#005CC5;--shiki-dark:#79B8FF\">500</span><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\"> });</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">  }</span></span>\n<span class=\"line\"><span style=\"--shiki-light:#24292E;--shiki-dark:#E1E4E8\">}</span></span></code></pre></figure><h2 id=\"why-the-release-line-is-there\" data-level=\"3\">Why The Release Line Is There</h2>\n<p>Vercel sets <code>VERCEL_GIT_COMMIT_SHA</code> on every deploy, and passing it as <code>release</code> means each error is stamped with the commit that was live when it happened. That matters once your coding agent is reading the errors. It can see which deploy introduced one, fix it, and resolve it with the commit hash of the fix. If the same error comes back, it reopens with the note from that fix attached.</p>\n<h2 id=\"check-it\" data-level=\"3\">Check It</h2>\n<p>The way I test a setup like this is to throw on purpose from a deployed preview, return the 500, and look for the event a moment later. If it never shows up, the flush isn’t running before the freeze. Runheed’s <code>runheed_send_test_event</code> tool sends a sample event from your agent, which is handy for checking the DSN and project, but it won’t tell you about the freeze. Only a real failure from the deployed function does.</p>\n<p>Any host that stops a process after it responds has the same problem, so check what yours offers for finishing work after the response. The Vercel setup is written up with the other frameworks in <a href=\"https://runheed.com/for/vercel?ref=deallocated-post\">Runheed’s docs</a>.</p>","created_at":"2026-11-29T14:00:00.000Z","updated_at":"2026-11-29T14:00:00.000Z"}