0
0
Vueframework~10 mins

Error boundaries with onErrorCaptured in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Error boundaries with onErrorCaptured
Child Component Throws Error
onErrorCaptured Hook in Parent
Return false
Stop Propagation
Parent Handles Error or Passes Up
When a child component throws an error, the parent's onErrorCaptured hook runs. Returning false stops error propagation; otherwise, it continues up.
Execution Sample
Vue
import { defineComponent, ref } from 'vue';

export default defineComponent({
  setup() {
    const errorMsg = ref('');
    function onErrorCaptured(err) {
      errorMsg.value = err.message;
      return false;
    }
    return { errorMsg, onErrorCaptured };
  }
});
A parent component uses onErrorCaptured to catch errors from children and store the error message.
Execution Table
StepEventError Thrown?onErrorCaptured Called?Return ValueError PropagationState Change
1Child renders normallyNoNoN/AN/AerrorMsg = ''
2Child throws error 'Oops!'YesYesfalseStoppederrorMsg = 'Oops!'
3Parent renders error messageNoNoN/AN/AerrorMsg = 'Oops!'
4Another error thrownYesYesfalseStoppederrorMsg = 'Another error!'
5Error propagation stopped, no further handlingN/AN/AN/AN/AerrorMsg holds last error
💡 Error propagation stops when onErrorCaptured returns false, preventing further error bubbling.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
errorMsg'''Oops!''Another error!''Another error!'
Key Moments - 2 Insights
Why does returning false in onErrorCaptured stop error propagation?
Returning false tells Vue the error is handled here, so it stops sending the error to parent components, as shown in step 2 of the execution_table.
What happens if onErrorCaptured returns true or nothing?
The error keeps propagating up to higher parents or global handlers, because only false stops propagation. This is implied by the flow in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of errorMsg after step 2?
A'Oops!'
B''
C'Another error!'
Dundefined
💡 Hint
Check the 'State Change' column at step 2 in the execution_table.
At which step does onErrorCaptured stop error propagation?
AStep 3
BStep 1
CStep 2
DStep 5
💡 Hint
Look at the 'Error Propagation' column in the execution_table.
If onErrorCaptured returned true instead of false, what would change in the execution_table?
AerrorMsg would not update
BError propagation would continue to parents
CError propagation would stop immediately
DChild would not throw error
💡 Hint
Refer to the 'Return Value' and 'Error Propagation' columns in the execution_table.
Concept Snapshot
Error boundaries in Vue use onErrorCaptured hook.
onErrorCaptured runs when a child error occurs.
Return false to stop error propagation.
Return true or nothing to continue bubbling.
Use to catch and handle errors gracefully.
Full Transcript
In Vue, error boundaries are created using the onErrorCaptured hook in a parent component. When a child component throws an error, Vue calls onErrorCaptured with the error. If the hook returns false, Vue stops sending the error to higher parents, effectively catching it. If it returns true or nothing, the error continues to propagate upwards. This lets you handle errors locally and prevent app crashes. The error message can be stored in a reactive variable to show feedback in the UI. This visual trace shows how errorMsg updates when errors occur and how returning false stops propagation.