Error boundaries help catch and handle errors in parts of your Vue app so the whole app doesn't break.
Error boundaries with onErrorCaptured in Vue
<script setup> import { onErrorCaptured } from 'vue' onErrorCaptured((error, instance, info) => { // handle or log the error return false // to stop error propagation }) </script>
onErrorCaptured is a Vue Composition API hook to catch errors in child components.
Returning false stops the error from propagating further up.
<script setup> import { onErrorCaptured } from 'vue' onErrorCaptured((error) => { console.error('Error caught:', error.message) return false }) </script>
<script setup> import { onErrorCaptured } from 'vue' onErrorCaptured((error, instance, info) => { alert(`Error in component: ${info}`) return false }) </script>
<script setup> import { onErrorCaptured } from 'vue' onErrorCaptured(() => { // ignore error and let it propagate return true }) </script>
This example shows a component that throws an error immediately. The ErrorBoundary catches it and shows a red error message instead of breaking the whole app.
<template>
<ErrorBoundary>
<BuggyComponent />
</ErrorBoundary>
</template>
<script setup>
import { defineComponent, ref, onErrorCaptured } from 'vue'
const BuggyComponent = defineComponent({
setup() {
const count = ref(0)
if (count.value === 0) {
throw new Error('Oops! BuggyComponent crashed.')
}
return { count }
},
template: `<div>Count: {{ count }}</div>`
})
const ErrorBoundary = defineComponent({
setup(props, { slots }) {
const errorMessage = ref('')
onErrorCaptured((error) => {
errorMessage.value = error.message
return false
})
return () => errorMessage.value
? <div role="alert" aria-live="assertive" style="color: red; font-weight: bold;">Error: {errorMessage.value}</div>
: slots.default()
}
})
</script>Use onErrorCaptured inside a component to catch errors from its children.
Always provide a fallback UI to keep your app usable when errors happen.
Remember to add role="alert" and aria-live="assertive" for accessibility when showing error messages.
Error boundaries catch errors in child components to prevent app crashes.
onErrorCaptured hook lets you handle errors in Vue's Composition API.
Return false to stop error propagation and show fallback UI.
Practice
onErrorCaptured hook in Vue?Solution
Step 1: Understand the role of error boundaries
Error boundaries catch errors in child components to prevent the whole app from crashing.Step 2: Identify what
This hook lets you catch those errors and handle them, for example by showing fallback UI.onErrorCaptureddoesFinal Answer:
To catch errors from child components and handle them gracefully -> Option DQuick Check:
Catches child errors gracefully [OK]
- Thinking it reloads the page automatically
- Believing it blocks all child rendering
- Assuming it only logs errors without UI impact
onErrorCaptured in a Vue 3 Composition API setup function?Solution
Step 1: Recall the correct syntax for hooks in Composition API
Hooks likeonErrorCapturedare called as functions with a callback.Step 2: Match the syntax with options
onErrorCaptured((error, instance, info) => { /* handle error */ }) correctly shows callingonErrorCapturedwith a callback function receiving error details.Final Answer:
onErrorCaptured((error, instance, info) => { /* handle error */ }) -> Option BQuick Check:
Hook usage = function call with callback [OK]
- Assigning onErrorCaptured instead of calling it
- Using object syntax inside setup incorrectly
- Calling non-existent methods on onErrorCaptured
ChildComponent throws an error?setup() {
onErrorCaptured((err) => {
console.log('Error caught:', err.message)
return false
})
}Solution
Step 1: Understand onErrorCaptured behavior with return false
Returning false stops error propagation and prevents further bubbling.Step 2: Analyze console.log call
The callback logs the error message, so console shows 'Error caught:' plus the error message.Final Answer:
Error caught: [error message] -> Option AQuick Check:
Return false stops propagation, logs error [OK]
- Assuming error propagates despite return false
- Expecting no console output
- Confusing syntax causing runtime errors
onErrorCaptured:setup() {
onErrorCaptured = (error) => {
console.error(error)
return false
}
}Solution
Step 1: Check how onErrorCaptured is used
It should be called as a function with a callback, not assigned to a function.Step 2: Identify the incorrect syntax
The code assigns a function to onErrorCaptured instead of calling it, causing an error.Final Answer:
Incorrect assignment instead of calling onErrorCaptured as a function -> Option AQuick Check:
Hooks are called, not assigned [OK]
- Assigning hook instead of calling it
- Forgetting to import onErrorCaptured
- Misunderstanding return false usage
onErrorCaptured to stop error propagation and display fallback UI?Solution
Step 1: Understand error boundary behavior
An error boundary catches errors and shows fallback UI by tracking error state.Step 2: Use onErrorCaptured to update error state and stop propagation
Returning false in onErrorCaptured stops error bubbling, allowing fallback UI to show.Final Answer:
Use onErrorCaptured to set a reactive error state and return false to stop propagation -> Option CQuick Check:
Set error state + return false = error boundary [OK]
- Not returning false, so errors keep propagating
- Trying to catch errors in template with try-catch
- Returning true which continues error propagation
Ready to prove it?
Pass the test and this pattern moves unknown
