OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Errors, stack traces and deliberate recovery

Read the failing operation and handle expected errors without turning them into fake success.

Lesson 15 of 25 · About 20 minutes · Beginner · English

By the end of this lesson

Throw an Error and catch it at a useful boundary.

Before you start

  • Node.js 24 with a terminal and plain-text editor; the browser-only lesson is labelled separately.
  • Complete lesson 14 or be comfortable with its examples.

Three words to know

Error
An object describing a failure.
Stack trace
A report of the call path around a thrown error.
Recovery
A deliberate alternative action when a known operation fails.

1. Report a rule violation

throw stops normal execution until a matching catch handles the failure. Throw an Error object rather than a bare string so useful stack information is available. A stack trace points to an operation and its callers; the line number is a clue to inspect, not a command to paste.

Check a supported statusJAVASCRIPT · Node.js · save as lesson.mjs
function requireStatus(value) {
  if (value !== "open" && value !== "closed") {
    throw new Error("Unsupported status");
  }
  return value;
}
try {
  console.log(requireStatus("pending"));
} catch (error) {
  console.log(error.message);
}
What you should see · example output
Unsupported status

2. Do not erase evidence

An empty catch block hides why something failed. Returning [] for a missing file might make a failure look like a valid empty report. In application code, distinguish errors you expect from defects that should remain visible. The final CLI uses a generic external message at its boundary, with tests to guard the internal rules.

3. Cleanup and output are separate decisions

A finally block runs as control leaves try/catch and is useful for closing a file. Avoid return in finally because it can hide a previous return or error. Validate all records before printing a summary; a later invalid record should not leave a plausible partial result.

YOUR TURN

A small check before you continue

If record 3 is invalid, should a report print results for records 1 and 2 first?

Show a suggested answer

Not when the contract promises whole-document validation. Build and validate first, then emit one result. A failure should remain distinguishable from a successful empty document.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.