Μετάβαση στο κύριο περιεχόμενο
← ⚡ JavaScript & the browser·Ενότητα C7 · Μάθημα 7
ΕργασίαWrite outer(inner) that wraps an awaited Promise with try/catch and re-throws a new Error('outer failed', { cause: original }). Call outer(Promise.reject(new TypeError('inner boom'))) inside try/catch and log err.message, err.cause.name, err.cause.message.

Error chaining with the cause option

125 XP8 min
Θεωρία

Wrapping low-level errors

When a low-level error happens deep in the stack, you usually want to add context before re-throwing — but you don't want to lose the original error. The { cause } option on Error does both:

async function loadProfile(userId) {
  try {
    return await db.findUser(userId);
  } catch (err) {
    throw new Error(`failed to load profile for ${userId}`, { cause: err });
  }
}

The caller catches the high-level error AND can drill into err.cause to inspect the original.

try {
  await loadProfile("u_123");
} catch (err) {
  console.error(err.message);        // "failed to load profile for u_123"
  console.error(err.cause?.message); // the underlying db error
}

Why not just re-throw?

Re-throwing keeps the message but loses the "where in the high-level flow did this happen" context. Wrapping adds business semantics; cause preserves the technical detail. Both layers stay available.

Stack vs cause

.stack shows where it was thrown. .cause shows what triggered it. Together they're invaluable in production logs.

🔒

Εγγράψου για να αρχίσεις να προγραμματίζεις

Η θεωρία είναι ανοιχτή για όλους. Ο επεξεργαστής, η ζωντανή προεπισκόπηση και ο έλεγχος ξεκλειδώνουν με 7-ήμερη δωρεάν δοκιμή — απαιτείται κάρτα.

Εγγραφή — δωρεάν δοκιμή →

Τα πρώτα 10 μαθήματα κάθε διαδρομής είναι δωρεάν. Δεν χρειάζεται κάρτα.

← ΠροηγούμενοΕπόμενο μάθημα →

Μια συμβουλή Python ή web την ημέρα — με email

Σύντομο, γραμμένο στο χέρι, χωρίς spam. Διαγραφή με ένα κλικ.