0
0
Vueframework~5 mins

Error boundaries with onErrorCaptured in Vue - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of onErrorCaptured in Vue?

onErrorCaptured is a Vue Composition API hook that catches errors from child components during rendering, lifecycle hooks, or watchers. It acts like an error boundary to prevent the whole app from crashing.

Click to reveal answer
beginner
How do you use onErrorCaptured in a Vue component?

Inside the setup() function, call onErrorCaptured((error, instance, info) => { ... }). Return true to stop the error from propagating further, or false or nothing to continue passing it up.

Click to reveal answer
intermediate
What arguments does the onErrorCaptured callback receive?
  • error: The actual error object thrown.
  • instance: The Vue component instance where the error happened.
  • info: A string describing the lifecycle hook or event during which the error occurred.
Click to reveal answer
intermediate
What happens if onErrorCaptured returns true?

Returning true tells Vue that the error was handled and stops the error from propagating to parent components or the global error handler.

Click to reveal answer
advanced
Can onErrorCaptured catch errors from asynchronous code like setTimeout?

No, onErrorCaptured only catches errors during rendering and lifecycle hooks. Errors in asynchronous callbacks like setTimeout need to be handled separately.

Click to reveal answer
Where do you place onErrorCaptured in a Vue component?
AInside the <code>methods</code> object
BInside the <code>template</code> section
CInside the <code>data()</code> function
DInside the <code>setup()</code> function
What does returning true from onErrorCaptured do?
ARestarts the component
BStops error propagation to parent components
CLogs the error to the console
DIgnores the error silently
Which of these errors can onErrorCaptured catch?
AErrors during component rendering
BErrors inside <code>setTimeout</code> callbacks
CNetwork request errors
DSyntax errors in JavaScript files
What arguments does the onErrorCaptured callback receive?
Acomponent, props, state
Bevent, data, context
Cerror, instance, info
Derror, message, stack
If onErrorCaptured returns false or nothing, what happens?
AError propagates to parent components
BError is ignored
CComponent reloads automatically
DError is logged and stopped
Explain how onErrorCaptured works as an error boundary in Vue components.
Think about how Vue lets you catch errors in child components to avoid app crashes.
You got /4 concepts.
    Describe a scenario where you would use onErrorCaptured and how you handle the error.
    Imagine a child component throws an error and you want the parent to catch it and show a message.
    You got /4 concepts.