Custom Error Classes की जानकारी
In this page:
class CustomError extends Error {
constructor(message: string) {
super(message);
this.name = "CustomError";
}
}
Creating a Custom Error
एक custom error class built-in Error को extend करती है और आमतौर पर this.name को एक specific string set करती है, जो logging और error-reporting tools को एक नज़र में exactly यह पहचानने देता है कि किस category की failure हुई।
उदाहरण: Creating a Custom Error
class NotFoundError extends Error {
constructor(message: string) {
super(message);
this.name = "NotFoundError";
}
}
try {
throw new NotFoundError("User not found");
} catch (err) {
if (err instanceof NotFoundError) console.log(err.name, err.message);
}
Custom Error Properties
Custom errors standard message के अलावा additional typed properties भी रख सकते हैं — एक error code, एक resource id, एक validation field name — catch blocks को सिर्फ एक string parse करने के बजाय act करने के लिए structured data देते हुए।
उदाहरण: Custom Error Properties
class ValidationError extends Error {
constructor(message: string, public field: string) {
super(message);
this.name = "ValidationError";
}
}
try {
throw new ValidationError("Required", "email");
} catch (err) {
if (err instanceof ValidationError) console.log(err.field, err.message);
}
Multiple Custom Errors
अलग-अलग error classes अलग failure categories represent कर सकती हैं — NotFoundError, ValidationError, AuthError — और अलग-अलग catch और handle की जा सकती हैं, ताकि हर तरह की failure को एक appropriately अलग response मिले।
उदाहरण: Multiple Custom Errors
class NotFoundError extends Error {}
class ValidationError extends Error {}
class AuthError extends Error {}
function handle(err: Error) {
if (err instanceof NotFoundError) console.log("404");
else if (err instanceof ValidationError) console.log("400");
else if (err instanceof AuthError) console.log("401");
}
handle(new ValidationError("bad input"));
Custom Errors in Functions
Functions custom errors throw कर सकते हैं जबकि callers instanceof checks उपयोग करके decide करते हैं कि कैसे respond करें, एक generic try/catch को specific, meaningful failure types पर एक dispatch table में बदलते हुए।
उदाहरण: Custom Errors in Functions
class NotFoundError extends Error {}
function findUser(id: number) {
if (id !== 1) throw new NotFoundError("User not found");
return { id, name: "Ravi" };
}
try {
findUser(2);
} catch (err) {
if (err instanceof NotFoundError) console.log("Handled:", err.message);
}
Best Practices for Custom Errors
Custom errors को meaningful application-level failures के लिए reserve करें जिन्हें distinguish करना worth हो, और उनकी extra properties को focused और strongly typed रखें बजाय हर error को एक loosely-typed grab bag बनाने के।
उदाहरण: Best Practices for Custom Errors
class PaymentError extends Error {
constructor(message: string, public code: number) {
super(message);
this.name = "PaymentError";
}
}
try {
throw new PaymentError("Card declined", 402);
} catch (err) {
if (err instanceof PaymentError) console.log(err.code, err.message);
}
- Constructor में
super(message)भूल जाना, जोthisउपयोग करने से पहले required है। this.nameset न करना, इसलिए error एक genericErrorके रूप में print होता है।- ES5 जैसे old targets में compile करते समय
instanceofसे catch करना, जहां prototype chain टूट जाती है जब तक आपObject.setPrototypeOfउपयोग न करें।
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: