Course contentsShow
← Web Development
Lesson 1668 of 2,98231. Error Handling and ResilienceFree lesson

Error vs ErrorInfo Objects

Understanding the error object and error info with component stack traces for debugging caught errors.

Error vs ErrorInfo Objects

You'll learn the difference between the standard error object and the framework-specific errorInfo object that React provides when an error boundary catches a component error.

What These Objects Are

When an error boundary catches an error in React, it receives two separate arguments to help you debug what went wrong:

  1. error - The standard JavaScript Error object that was thrown
  2. errorInfo - A React-specific object containing the component stack trace

Think of it like a crime scene investigation: the error object tells you what went wrong (the crime), while errorInfo tells you where it happened in your component tree (the location).

The Error Object

The standard error object contains:

  • error.message - Human-readable description of what failed
  • error.name - Type of error (TypeError, ReferenceError, etc.)
  • error.stack - JavaScript call stack showing the line of code that threw

The ErrorInfo Object

The errorInfo object provides:

  • errorInfo.componentStack - A string showing the React component hierarchy where the error occurred

This component stack is crucial because it shows you which component rendered which, helping you trace the error from the failing component back up to the error boundary.

componentDidCatch(error, errorInfo) {
  console.log('Error message:', error.message);
  console.log('Component path:', errorInfo.componentStack);
  // Shows something like:
  //   in UserProfile
  //   in Dashboard  
  //   in App
}

Key Takeaway: The error object tells you what broke and where in your JavaScript code, while errorInfo tells you which React components were involved in rendering when it broke—use both together for complete debugging context.