TypeScript में उन्नत त्रुटि प्रबंधन: try/catch से टाइप-सुरक्षित समाधानों तक
त्रुटि प्रबंधन TypeScript में विकास का एक महत्वपूर्ण पहलू है। बुनियादी try/catch अक्सर जटिल अनुप्रयोगों के लिए अपर्याप्त होता है। इस लेख में, हम उन्नत तकनीकों पर चर्चा करेंगे जो कोड को अधिक विश्वसनीय, पूर्वानुमानित और टाइप-सुरक्षित बनाने में मदद करेंगी।
1. मानक try/catch की समस्या: प्रकारों का नुकसान
क्लासिक catch का मुख्य दोष यह है कि त्रुटि चर का प्रकार unknown (या पुराने संस्करणों में any) होता है। यह डेवलपर को मैन्युअल रूप से प्रकार जांचने के लिए मजबूर करता है:
try { riskyOperation();} catch (error) { // error: unknown if (error instanceof Error) { console.error(error.message); } else { console.error('अज्ञात त्रुटि', error); }}समाधान: टाइप किए गए त्रुटि कैप्चर के लिए उपयोगिता बनाना:
function getErrorMessage(error: unknown): string { if (error instanceof Error) return error.message; if (typeof error === 'string') return error; if (error && typeof error === 'object' && 'message' in error) { return String((error as { message: unknown }).message); } return 'एक अज्ञात त्रुटि हुई';}
try { riskyOperation();} catch (error) { console.error(getErrorMessage(error));}2. टाइप किए गए डेटा के साथ कस्टम त्रुटि क्लासेज़
त्रुटियों का पदानुक्रम बनाने से समस्या की सटीक पहचान और अतिरिक्त डेटा संचारित करने में मदद मिलती है:
// बेस क्लासclass AppError extends Error { constructor( message: string, public readonly code: number, public readonly isOperational: boolean = true ) { super(message); this.name = 'AppError'; }}
// विशिष्ट त्रुटियाँclass ValidationError extends AppError { constructor( message: string, public readonly field: string, public readonly invalidValue: unknown ) { super(message, 400); this.name = 'ValidationError'; }}
class NotFoundError extends AppError { constructor(entity: string, id: string) { super(`${entity} ID ${id} के साथ नहीं मिला`, 404, false); this.name = 'NotFoundError'; }}
// उपयोगfunction findUser(id: string): User { if (!validateId(id)) { throw new ValidationError('अमान्य ID', 'id', id); } const user = db.findUser(id); if (!user) { throw new NotFoundError('उपयोगकर्ता', id); } return user;}
try { const user = findUser('123');} catch (error) { if (error instanceof NotFoundError) { // error.code === 404, error.message में विवरण है console.warn(error.message); } else if (error instanceof ValidationError) { // error.field और error.invalidValue उपलब्ध हैं console.error(`फ़ील्ड ${error.field} में त्रुटि: ${error.message}`); } else if (error instanceof AppError) { console.error(`एप्लिकेशन त्रुटि (कोड ${error.code}): ${error.message}`); }}3. त्रुटि प्रबंधन के लिए Discriminated Unions
यह पैटर्न अपवादों के बिना संचालन के परिणामों को संसाधित करने की अनुमति देता है, विभेदक के साथ प्रकारों के संघ का उपयोग करके:
// विभेदक के साथ परिणाम प्रकारtype Result<T, E = Error> = | { success: true; data: T } | { success: false; error: E };
// Result लौटाने वाला फ़ंक्शनfunction safeDivide(a: number, b: number): Result<number, string> { if (b === 0) { return { success: false, error: 'शून्य से भाग' }; } return { success: true, data: a / b };}
// उपयोगconst result = safeDivide(10, 0);
if (result.success) { // TypeScript जानता है कि result.data — number है console.log(`परिणाम: ${result.data}`);} else { // result.error — string है console.error(`त्रुटि: ${result.error}`);}
// कस्टम त्रुटियों के साथ अधिक जटिल उदाहरणtype UserResult = Result<