📖 Scenario: You are building a Vue app that shows user profiles. Sometimes, a profile might have missing or broken data that causes errors. You want to catch these errors gracefully and show a friendly message instead of a broken page.
🎯 Goal: Create a Vue component that uses onErrorCaptured to catch errors from a child component and display a fallback message.
📋 What You'll Learn
Create a child component called UserProfile that throws an error when the user prop is missing.
Create a parent component called ErrorBoundary that uses onErrorCaptured to catch errors from UserProfile.
Show a fallback message inside ErrorBoundary when an error is caught.
Pass a user prop to UserProfile from ErrorBoundary.
💡 Why This Matters
🌍 Real World
Error boundaries help keep your app running smoothly by catching unexpected errors in parts of your UI and showing friendly messages instead of crashing the whole page.
💼 Career
Understanding error boundaries is important for building robust Vue applications that handle failures gracefully and improve user experience.
Progress0 / 4 steps
1
Create the UserProfile component
Create a Vue component called UserProfile that accepts a prop named user. If user is not provided, throw an error with the message 'User data is missing'. Otherwise, render a <div> showing User: {{ user.name }}.
Vue
Hint
Use defineProps to declare the user prop. Check if user is missing and throw an error. Render the user's name inside a <div>.
2
Create the ErrorBoundary component setup
Create a Vue component called ErrorBoundary. Inside <script setup>, import ref and onErrorCaptured from 'vue'. Create a reactive variable called error initialized to null. Use onErrorCaptured to catch errors and set error.value to the error message.
Vue
Hint
Use ref(null) to create error. Use onErrorCaptured callback to catch errors and update error.value.
3
Render UserProfile and show fallback on error
Inside the ErrorBoundary template, use a <template> with v-if to show a <div> with text 'Error: ' + error if error is not null. Otherwise, render the UserProfile component and pass a user prop with value { name: 'Alice' }. Import UserProfile at the top.
Vue
Hint
Use v-if to show the error message. Use v-else to show UserProfile with the user prop.
4
Test error handling by passing no user
Modify the ErrorBoundary template to render UserProfile without the user prop to trigger the error. Keep the error message display logic unchanged.
Vue
Hint
Remove the user prop from UserProfile to cause the error and see the fallback message.
Practice
(1/5)
1. What is the main purpose of the onErrorCaptured hook in Vue?
easy
A. To prevent any rendering of child components
B. To automatically reload the page when an error occurs
C. To log errors only in the browser console without affecting UI
D. To catch errors from child components and handle them gracefully
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 onErrorCaptured does
This hook lets you catch those errors and handle them, for example by showing fallback UI.
Final Answer:
To catch errors from child components and handle them gracefully -> Option D
Quick Check:
Catches child errors gracefully [OK]
Hint: Remember: onErrorCaptured catches child errors for graceful handling [OK]
Common Mistakes:
Thinking it reloads the page automatically
Believing it blocks all child rendering
Assuming it only logs errors without UI impact
2. Which of the following is the correct way to use onErrorCaptured in a Vue 3 Composition API setup function?
easy
A. onErrorCaptured.handleError(error => { /* handle error */ })
A. Incorrect assignment instead of calling onErrorCaptured as a function
B. Missing import of onErrorCaptured from Vue
C. Returning false is not allowed in onErrorCaptured
D. console.error cannot be used inside setup
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 A
Quick Check:
Hooks are called, not assigned [OK]
Hint: Use onErrorCaptured(callback), not assignment [OK]
Common Mistakes:
Assigning hook instead of calling it
Forgetting to import onErrorCaptured
Misunderstanding return false usage
5. You want to create an error boundary component in Vue 3 that catches errors from its children and shows a fallback message. Which approach correctly uses onErrorCaptured to stop error propagation and display fallback UI?
hard
A. Wrap children in try-catch inside template to catch errors
B. Use onErrorCaptured but do not return anything, so errors propagate
C. Use onErrorCaptured to set a reactive error state and return false to stop propagation
D. Use onErrorCaptured and always return true to propagate errors
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 C
Quick Check:
Set error state + return false = error boundary [OK]
Hint: Return false in onErrorCaptured to stop error bubbling [OK]
Common Mistakes:
Not returning false, so errors keep propagating
Trying to catch errors in template with try-catch
Returning true which continues error propagation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.