← Back to TypeScript Course | Chapter 18: Error Handling | Lesson 3 of 5

Error Boundaries का Concept

एक error boundary एक design pattern है जो failures को एक defined application boundary पर contain करता है बजाय उन्हें program के unrelated parts को crash करने देने के। TypeScript एक universal error-boundary keyword प्रदान नहीं करता, इसलिए boundaries try/catch, middleware, या framework-specific features जैसे mechanisms से implement की जाती हैं।
Syntax
typescript
function boundary<T>(fn: () => T, fallback: T): T {
    try {
        return fn();
    } catch {
        return fallback;   // failure becomes a controlled result
    }
}

What Is an Error Boundary?

एक error boundary application में एक defined point है जहां एक failure catch, log, और एक controlled result में convert की जाती है बजाय call stack में अनpredictably आगे crash या propagate होने के।

उदाहरण: What Is an Error Boundary?

typescript
function boundary<T>(fn: () => T, fallback: T): T {
  try {
    return fn();
  } catch {
    return fallback;
  }
}
console.log(boundary(() => { throw new Error("fail"); }, "default value"));

Boundaries Around Services

एक service call के आस-पास रखी एक boundary low-level failures को रोक सकती है — एक database timeout, एक malformed API response — raw implementation details को higher-level application code में leak होने से जिसे यह जानने की ज़रूरत नहीं होनी चाहिए।

उदाहरण: Boundaries Around Services

typescript
function callService(): string {
  throw new Error("Database timeout");
}
function boundary<T>(fn: () => T, fallback: T): T {
  try {
    return fn();
  } catch {
    return fallback;
  }
}
console.log(boundary(callService, "service unavailable"));

Boundaries in HTTP Applications

Web applications आमतौर पर request handling के आस-पास एक error boundary रखती हैं ताकि request के दौरान कोई भी failure एक unhandled crash के बजाय एक controlled HTTP response (जैसे एक safe message के साथ 500) बन जाए।

उदाहरण: Boundaries in HTTP Applications

typescript
function handleRequest(): { status: number; body: string } {
  try {
    throw new Error("unexpected failure");
  } catch {
    return { status: 500, body: "Internal Server Error" };
  }
}
console.log(handleRequest());

Boundaries and Logging

एक अच्छी error boundary को debugging के लिए internally enough detail record करना चाहिए — stack trace, request context — जबकि end user या external caller को एक कहीं ज़्यादा safe, less detailed message return करते हुए।

उदाहरण: Boundaries and Logging

typescript
function handleRequest() {
  try {
    throw new Error("db connection lost");
  } catch (err) {
    console.log("[internal log]", err); // detailed, for developers
    return { status: 500, body: "Something went wrong" }; // safe, for users
  }
}
console.log(handleRequest());

Designing Useful Boundaries

Boundaries को एक meaningful architectural level पर रखें, errors को इतनी silently swallow करने से बचें कि real bugs unnoticed रह जाएं, और एक ऐसी boundary के लिए aim करें जो दोनों तरह से एक predictable, well-typed result return करे।

उदाहरण: Designing Useful Boundaries

typescript
function boundary<T>(fn: () => T, fallback: T): T {
  try {
    return fn();
  } catch (err) {
    console.log("Caught at boundary:", err);
    return fallback;
  }
}
console.log(boundary(() => JSON.parse("not json"), null));
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक error catch करना और उसके साथ कुछ न करना, इसलिए problems unnoticed रह जाती हैं।
  2. Users को safe message के बजाय stack traces जैसे internal details return करना।
  3. हर छोटी line को अपनी boundary में wrap करना बजाय एक meaningful edge पर एक के।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.