Course contentsShow
← JavaScript
Lesson 1356 of 2,72330. Async/AwaitFree lesson

Extracting Error Information

Access error message, stack, and custom properties from caught exceptions in async functions.

Extracting Error Information

You'll learn: how to access and use the details inside error objects caught in async/await try/catch blocks.

The Core Idea

When you catch an error in a try/catch block around an await, you're catching an actual JavaScript Error object. This object isn't just a simple message—it's a rich data structure containing multiple properties that help you understand what went wrong, where it happened, and sometimes even custom information the code that threw it wanted to pass along.

Think of an error object like a police incident report: it has the basic description (the message), evidence of the location (the stack trace), and sometimes additional notes (custom properties). Knowing how to read all these fields gives you much better debugging power.

Key Properties to Extract

The standard Error object provides:

  • error.message: The human-readable description of what went wrong
  • error.stack: A multi-line string showing the call stack (function call history) leading to the error
  • Custom properties: Any additional data attached by the code that created the error
async function fetchUserData(userId) {
  throw new Error('User not found');
}

async function main() {
  try {
    await fetchUserData(42);
  } catch (error) {
    console.log('Message:', error.message);      // "User not found"
    console.log('Stack trace:', error.stack);    // Full stack trace string
    console.log('Error name:', error.name);      // "Error"
    console.log('Status code:', error.statusCode); // undefined (unless set)
  }
}

Key Takeaway: Caught error objects contain message, stack, name, and any custom properties—extract these to log meaningful debugging information or handle different error types appropriately.