Chapter 103: Vue ‘errorCaptured’ Lifecycle Hook

ErrorCaptured lifecycle hook

This hook is not like mounted or onBeforeUnmount that you use every day. It is a special emergency hook — the only built-in mechanism Vue gives you to catch JavaScript errors that occur in descendant components (children, grandchildren, etc.) during the rendering phase, lifecycle hooks, and event handlers.

In plain words:

When any child component (or deeper descendant) throws an uncaught error during render / lifecycle / event handling, Vue will automatically call errorCaptured on every ancestor component that has defined it — starting from the component closest to the error and walking up the tree.

If no one catches the error → it bubbles up to the global app.config.errorHandler (if set) → otherwise → Vue logs it to console and the app crashes / white screen of death.

1. When does errorCaptured get called? (exact situations)

Vue calls errorCaptured in these four specific cases:

  1. Error during render of a descendant component (render() function throws, template compilation error that surfaces during render)
  2. Error in a lifecycle hook of a descendant (except errorCaptured itself and unmounted)
  3. Error in a watcher (watch, watchEffect) of a descendant
  4. Error in an event handler (@click, custom emitted event handler) of a descendant

Important exclusions:

  • Errors in your own component’s render / hooks / watchers → not caught by your own errorCaptured
  • Errors in asynchronous code (inside setTimeout, Promise.then, async functions) → not caught
  • Errors in custom directives lifecycle → not caught
  • Errors in global error handler itself → not caught

2. Signature & Arguments (what you receive)

In Options API:

JavaScript

In Composition API (<script setup>):

TypeScript

Arguments explained:

Argument Type What it contains
err Error The actual JavaScript error object (err.message, err.stack)
instance ComponentInternalInstance The component instance that threw the error (the child/grandchild)
info string String describing where the error occurred (very useful for debugging)

Common info values you will see:

  • “render function”
  • “setup function”
  • “mounted hook”
  • “updated hook”
  • “watcher callback”
  • “event handler”
  • “vnode hook” (directives, transitions…)
  • “component event handler” (emitted custom event)

3. Real, Practical Example – Error Boundary Component

vue

Parent usage – wrapping dangerous component

vue

BuggyComponent.vue (deliberately broken)

vue

Result:

  • BuggyComponent throws during render
  • ErrorBoundary catches it via onErrorCaptured
  • Shows nice fallback UI
  • Rest of the app does not crash — error is contained

4. Important Rules & Gotchas (2026 Must-Know)

Rule / Gotcha Correct Behavior / Best Practice
Catches errors in descendants only Not your own component’s render/hooks — only children & deeper
Propagation Return true → stop propagating upward Return false → let it continue up the tree
Multiple boundaries Vue calls errorCaptured on every ancestor that has it — from closest to root
Does not catch async errors setTimeout, Promise.then, async functions — use try/catch inside them
Does not catch errors in event handlers of parent Only descendant event handlers
Does not prevent app crash if unhandled If no one returns true → error bubbles to app.config.errorHandler → then console
Global fallback Set app.config.errorHandler = (err, instance, info) => { … } for uncaught errors
Still used in 2026? Yes — in UI libraries, error boundaries, dashboard wrappers, mission-critical apps

5. Modern Pattern – Reusable ErrorBoundary Component (2026 standard)

Almost every serious Vue 3 app has something like this:

vue

→ Wrap any potentially unstable subtree:

vue

Quick Summary Table – beforeUnmount vs unmounted vs errorCaptured

Hook Purpose DOM still alive? Cleanup here? Catches child errors?
onBeforeUnmount Last chance to clean up before removal Yes Yes No
onUnmounted Component is gone – final logging/telemetry No No No
onErrorCaptured Catch render/lifecycle/event errors in children Yes No Yes

Pro Tips from Real Projects (Hyderabad 2026)

  • Always use onBeforeUnmount for cleanup — onUnmounted is too late
  • Wrap risky parts of your app in <ErrorBoundary> components — prevents full app crash
  • Combine onErrorCaptured with Sentry / LogRocket / Bugsnag — send errors with component name & info
  • In SSR / Nuxt → onErrorCaptured runs only client-side
  • Test error boundaries — throw errors in child components → make sure fallback UI appears & app doesn’t crash
  • Return true in onErrorCaptured → stop propagation — prevents error from reaching global handler

Your mini homework:

  1. Create ErrorBoundary.vue as shown
  2. Wrap a buggy component that throws in render (null.toUpperCase())
  3. Toggle it with v-if → see error is caught, fallback shown, rest of app alive
  4. Remove return true → see error propagates (console error appears)

Any part confusing? Want full examples for:

  • onErrorCaptured + Sentry integration?
  • Error boundary with reset + retry logic?
  • errorCaptured vs global app.config.errorHandler?
  • Error handling in <Suspense> / <Transition> / <KeepAlive>?

Just tell me — we’ll build the next robust, crash-proof component together 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *