जावास्क्रिप्ट में एडवांस एरर हैंडलिंग: try/catch से ग्लोबल हैंडलर तक
\n\nएरर हैंडलिंग का मतलब सिर्फ कोड को try/catch में लपेटना नहीं है। Node.js पर चलने वाले रियल-टाइम प्रोडक्शन ऐप्लिकेशन में, सही तरीके से एरर मैनेज करने से सर्वर क्रैश से बचा जा सकता है, समस्याओं को सही तरीके से लॉग किया जा सकता है और यूज़र को समझने योग्य मैसेज दिए जा सकते हैं। इस आर्टिकल में हम जावास्क्रिप्ट की उन्नत एरर हैंडलिंग तकनीकों को समझेंगे जो बेसिक सिंटैक्स से आगे जाती हैं।
\n\n1. try/catch और finally की गहराई से समझ
\n\nक्लासिक try...catch...finally कंस्ट्रक्शन सिंक्रोनस एरर हैंडलिंग की नींव है। लेकिन एडवांस लेवल पर इसके काम करने के तरीके को समझना ज़रूरी है।
function riskyOperation(value) {\n if (typeof value !== 'number') {\n throw new TypeError('Argument must be a number');\n }\n if (value < 0) {\n throw new RangeError('Value cannot be negative');\n }\n return Math.sqrt(value);\n}\n\ntry {\n const result = riskyOperation(-4);\n console.log('Result:', result);\n} catch (error) {\n if (error instanceof TypeError) {\n console.error('Type Error:', error.message);\n } else if (error instanceof RangeError) {\n console.error('Range Error:', error.message);\n } else {\n console.error('Unknown error:', error);\n }\n} finally {\n console.log('Cleanup: always executed');\n}\n\nमुख्य बिंदु:
\n- \n
finallyब्लॉक हमेशा चलता है, भले हीtryयाcatchमेंreturnहो। \n - सटीक पहचान के लिए बिल्ट-इन एरर क्लासेज़ (
TypeError,RangeError,SyntaxError) का उपयोग करें। \n - उन एरर को न पकड़ें जिन्हें आप हैंडल नहीं कर सकते — उन्हें ऊपर जाने दें। \n
2. कस्टम एरर क्लासेज़ और Error को एक्सटेंड करना
\n\nबड़े प्रोजेक्ट्स के लिए स्टैंडर्ड एरर टाइप्स काफी नहीं होते। अपनी खुद की क्लासेज़ बनाकर आप मेटाडेटा (HTTP-स्टेटस, एरर कोड, कॉन्टेक्स्ट) जोड़ सकते हैं और फ़िल्टरिंग आसान बना सकते हैं।
\n\nclass AppError extends Error {\n constructor(message, statusCode, code) {\n super(message);\n this.name = 'AppError';\n this.statusCode = statusCode;\n this.code = code;\n Error.captureStackTrace(this, this.constructor);\n }\n}\n\nclass ValidationError extends AppError {\n constructor(message, field) {\n super(message, 400, 'VALIDATION_ERROR');\n this.field = field;\n }\n}\n\nclass DatabaseError extends AppError {\n constructor(message, query) {\n super(message, 500, 'DB_ERROR');\n this.query = query;\n }\n}\n\n// उपयोग\ntry {\n throw new ValidationError('Email is required', 'email');\n} catch (error) {\n if (error instanceof ValidationError) {\n console.log(`Field: ${error.field}, Status: ${error.statusCode}`);\n }\n}\n\nयह क्यों ज़रूरी है: कस्टम एरर आपको अलग-अलग सिचुएशन को सेंट्रलाइज़्ड तरीके से हैंडल करने (जैसे Express.js middleware में) और क्लाइंट को स्ट्रक्चर्ड JSON भेजने की सुविधा देते हैं।
\n\n3. एसिंक्रोनस कोड में एरर हैंडलिंग: async/await और Promise
\n\nNode.js में ज़्यादातर कोड एसिंक्रोनस होता है। Promise और async/await में एरर को हैंडल करने के लिए खास तरीका चाहिए।
3.1. Promise को हैंडल करना
\n\nfunction fetchUserData(userId) {\n return new Promise((resolve, reject) => {\n if (userId <= 0) {\n reject(new RangeError('Invalid user ID'));\n }\n // डेटाबेस क्वेरी का अनुकरण\n setTimeout(() => resolve({ id: userId, name: 'Alice' }), 100);\n });\n}\n\nfetchUserData(-1)\n .then(user => console.log(user))\n .catch(error => {\n console.error('Promise rejected:', error.message);\n })\n .finally(() => console.log('Cleanup after promise'));\n\n3.2. async/await को हैंडल करना
\n\nasync function getUserProfile(userId) {\n try {\n const user = await fetchUserData(userId);\n const posts = await fetchUserPosts(user.id