Vibe Code Guardian
← All posts

Error Handling in AI-Generated Code: What Your Vibe-Coded App Is Missing When Things Go Wrong

Your AI-built app works perfectly — until it doesn't. Most AI coding tools skip error boundaries, structured logging, and monitoring. Here is what to add before real users hit the edge cases.

Aaron Allen·

The happy path is not the whole app

You asked Cursor to build a feature. It wrote the code, you tested the happy path, and it worked. You deployed.

Three weeks later a user emails: "I tried to upload a PDF and the screen went white. Now I can't do anything." You check the logs. There are no logs. The error vanished into a catch block that does nothing.

This is the default for AI-generated code. The model optimises for the feature you described. It does not optimise for what happens when the database times out, the payment provider returns a 500, or a user uploads a 500 MB file when you expected 5 MB.

The gap between "it works" and "it survives" is where AI-built apps fail.

What AI tools consistently miss

Error boundaries that actually catch things

In React, Next.js, or Vue, an error boundary stops a component crash from taking down the whole page. AI tools rarely add them unless you explicitly ask.

A typical AI-generated component:

// What the AI writes
const Dashboard = async () => {
  const data = await fetchUserData(userId)
  return <Charts data={data} />
}

If fetchUserData throws, the entire page crashes. No fallback UI. No error reported. The user sees a blank screen.

What should be there:

// What you need
const Dashboard = () => (
  <ErrorBoundary fallback={<DashboardError />}> 
    <DashboardContent />
  </ErrorBoundary>
)

const DashboardContent = async () => {
  const data = await fetchUserData(userId)
  return <Charts data={data} />
}

The boundary catches the error, shows a useful fallback, and — critically — reports the error somewhere you can see it.

Structured logging you can query

console.log("error", error) is not logging. It is noise.

AI tools write logs that are:

  • Unstructured (plain text, inconsistent formats)

  • Missing context (no user ID, request ID, feature name)

  • Scattered across console.log, console.error, and nothing at all

  • Lost when the serverless function cold-starts

What you need instead:

// Structured log entry you can filter and alert on
logger.error("payment.webhook.failed", {
  userId: user.id,
  stripeEventId: event.id,
  errorCode: err.code,
  errorMessage: err.message,
  requestId: headers.get("x-request-id")
})

Every log entry should have: timestamp, level, event name, user context, request ID, and the error details. Then you can query "show me all payment.webhook.failed in the last hour" instead of grepping through text files.

Alerting that wakes you up

No logs means no alerts. No alerts means you find out from users.

The minimum viable alerting for a vibe-coded app:

  1. Error rate spike — if 5xx errors exceed 1 percent of requests in 5 minutes, notify

  2. Critical path failures — payment webhook failures, auth callback failures, database connection failures

  3. Queue depth — if background jobs pile up, something is stuck

  4. Disk or memory pressure — the boring infrastructure things that kill apps

You do not need Datadog or New Relic on day one. A simple setup:

  • Sentry or Logtail for error capture and alerting (free tiers cover small apps)

  • UptimeRobot or Better Stack for uptime checks on critical endpoints

  • Cron job that pings your health endpoint and alerts on failure

Monitoring the AI itself

This is specific to vibe coding. Your AI makes changes. You accept them. Who watches the AI?

  • Dependency drift — the AI adds a package. A week later a CVE is published. You need npm audit or dependabot running automatically

  • Schema drift — the AI changes a database column type. The migration runs. The old code still expects the old type. You need schema validation in CI

  • Secret rotation — the AI commits a key. You rotate it. The AI commits the new one. You need git history scanning (truffleHog, gitleaks) in pre-commit and CI

The launch-readiness audit includes a section on your AI workflow: what the AI can touch, and whether anyone checks what it changes.

A practical checklist you can run this week

1. Add error boundaries to every top-level route

In Next.js App Router, wrap each page segment in an error.tsx. In React Router, wrap routes in <ErrorBoundary>. In Vue, use onErrorCaptured. The fallback UI should show the user what happened and what to do next ("Something went wrong. We've been notified. Try refreshing or contact support.").

2. Replace every console.log in error paths with structured logging

Pick a logger (Pino, Winston, or the structured logging in your framework). Define a schema: level, event, userId, requestId, metadata. Use it everywhere an error can occur: API routes, server actions, webhook handlers, background jobs.

3. Set up error alerting

Connect Sentry (or similar) to Slack or email. Configure alerts for:

  • Any unhandled exception

  • Error rate above 1 percent in 5 minutes

  • Specific critical events (payment failures, auth failures)

4. Add a health endpoint that checks dependencies

// GET /health
const checks = await Promise.allSettled([
  db.$queryRaw`SELECT 1`,
  redis.ping(),
  stripe.webhookEndpoints.list({ limit: 1 }),
  resend.emails.send({ from: '[email protected]', to: '[email protected]', subject: 'health', html: 'ok' })
])

const healthy = checks.every(c => c.status === 'fulfilled')
return healthy ? 200 : 503

Point UptimeRobot at it. Alert on 503.

5. Enable dependency scanning

Turn on GitHub Dependabot or add npm audit to CI. Set it to auto-PR for security fixes. The AI will keep adding packages. You need the safety net.

6. Test the failure paths

Do not just test that the feature works. Test:

  • What happens when the database is slow? (Add artificial latency in staging)

  • What happens when the payment provider returns an error? (Use test mode error codes)

  • What happens when the user loses connectivity mid-request?

  • What happens when a background job fails three times?

The free surface check catches the visible gaps

Run the free surface check on your deployed domain. It looks at what the browser can see: missing security headers, exposed files, certificate issues. It does not check your error handling or monitoring — those are inside the app — but it is a fast way to catch the configuration gaps that AI tools also miss.

You do not need to build all of this yourself

If you would rather a developer review your error handling, logging, and monitoring setup, the launch-readiness audit covers 137 points across 14 areas including "Payments and errors", "Monitoring", and "Your AI workflow". A developer confirms every finding and you get a ranked must-fix list.

The goal is not perfect code. The goal is knowing when something breaks — before your users tell you.

Want a developer to look at your app?

Built your app with AI tools? Run the free surface check yourself in a few minutes. Nothing to buy, and we never look at your app unless you run it.