What if a tiny error could break your whole app -- and how to stop that from happening?
Why Error boundaries with onErrorCaptured in Vue? - Purpose & Use Cases
Imagine building a Vue app where a small mistake in one component crashes the whole page, leaving users staring at a blank screen with no clue what went wrong.
Without error boundaries, errors bubble up and break the entire app. Debugging is hard because the app stops working suddenly, and users get a bad experience with no helpful message.
Vue's onErrorCaptured lets you catch errors inside components gracefully. You can show fallback UI or log errors without crashing the whole app, keeping your app stable and user-friendly.
export default { mounted() { throw new Error('Oops!') } }import { onErrorCaptured } from 'vue'; export default { setup() { onErrorCaptured(err => { console.error(err); return true; }); } }
This lets your app handle unexpected errors smoothly, improving reliability and user trust.
Think of an online store where a product detail component fails to load. Instead of crashing the whole page, you show a friendly message and let users keep browsing other products.
Errors in components can crash the whole Vue app.
onErrorCaptured catches errors locally to prevent crashes.
This improves app stability and user experience.
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
