Error Boundaries का Concept
In this page:
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?
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
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
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
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
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));
- एक error catch करना और उसके साथ कुछ न करना, इसलिए problems unnoticed रह जाती हैं।
- Users को safe message के बजाय stack traces जैसे internal details return करना।
- हर छोटी line को अपनी boundary में wrap करना बजाय एक meaningful edge पर एक के।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: