You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: question.json
+80Lines changed: 80 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -4638,5 +4638,85 @@
4638
4638
"level": "advanced",
4639
4639
"theme": "Protractor, Headless Testing",
4640
4640
"text": "### How do you run Protractor tests in headless mode?\n\nTo run Protractor tests in headless mode, you can configure the `protractor.conf.js` file to use a headless browser like Chrome.\n\nExample configuration:\n\n```javascript\ncapabilities: {\n 'browserName': 'chrome',\n 'chromeOptions': {\n args: ['--headless', '--disable-gpu', '--window-size=800x600']\n }\n},\n```"
4641
+
},
4642
+
{
4643
+
"link": "#how-to-catch-errors-in-promises",
4644
+
"title": "How to catch errors in Promises",
4645
+
"url": "",
4646
+
"level": "beginner",
4647
+
"theme": "JavaScript, Promises, Error Handling",
4648
+
"text": "### How to catch errors in Promises\n\nYou can handle errors in promises by appending a `.catch()` method to the promise chain.\n\n```javascript\nfetch('https://api.example.com/data')\n .then(response => response.json())\n .then(data => console.log(data))\n .catch(error => console.error('Error:', error));\n```"
"text": "### How to catch errors in async/await\n\nWrap your async code in a try/catch block to handle errors gracefully.\n\n```javascript\nasync function fetchData() {\n try {\n const response = await fetch('https://api.example.com/data');\n const data = await response.json();\n console.log(data);\n } catch (error) {\n console.error('Error:', error);\n }\n}\nfetchData();\n```"
"title": "How to catch errors in synchronous code",
4661
+
"url": "",
4662
+
"level": "beginner",
4663
+
"theme": "JavaScript, Error Handling",
4664
+
"text": "### How to catch errors in synchronous code\n\nFor synchronous operations, use a try/catch block to handle errors.\n\n```javascript\ntry {\n const result = riskyOperation();\n console.log(result);\n} catch (error) {\n console.error('Caught an error:', error);\n}\n```"
4665
+
},
4666
+
{
4667
+
"link": "#how-to-catch-errors-in-event-handlers",
4668
+
"title": "How to catch errors in event handlers",
4669
+
"url": "",
4670
+
"level": "intermediate",
4671
+
"theme": "JavaScript, Error Handling, Events",
4672
+
"text": "### How to catch errors in event handlers\n\nWrap event handler code in a try/catch block to ensure errors do not crash the application.\n\n```javascript\ndocument.getElementById('myButton').addEventListener('click', () => {\n try {\n // Code that might throw an error\n performAction();\n } catch (error) {\n console.error('Error during click event:', error);\n }\n});\n```"
4673
+
},
4674
+
{
4675
+
"link": "#how-to-catch-errors-with-fetch-api",
4676
+
"title": "How to catch errors with the Fetch API",
4677
+
"url": "",
4678
+
"level": "beginner",
4679
+
"theme": "JavaScript, Fetch API, Error Handling",
4680
+
"text": "### How to catch errors with the Fetch API\n\nUse `.catch()` to handle errors when making HTTP requests with the Fetch API.\n\n```javascript\nfetch('https://api.example.com/data')\n .then(response => {\n if (!response.ok) {\n throw new Error('Network response was not ok');\n }\n return response.json();\n })\n .then(data => console.log(data))\n .catch(error => console.error('Fetch error:', error));\n```"
4681
+
},
4682
+
{
4683
+
"link": "#how-to-catch-errors-in-nodejs",
4684
+
"title": "How to catch errors in Node.js",
4685
+
"url": "",
4686
+
"level": "intermediate",
4687
+
"theme": "JavaScript, Node.js, Error Handling",
4688
+
"text": "### How to catch errors in Node.js\n\nIn Node.js, use try/catch for synchronous code and attach error listeners for asynchronous code.\n\n```javascript\n// Synchronous error handling\ntry {\n const data = fs.readFileSync('/path/to/file');\n console.log(data);\n} catch (error) {\n console.error('Error reading file:', error);\n}\n\n// Asynchronous error handling\nfs.readFile('/path/to/file', (error, data) => {\n if (error) {\n return console.error('Error reading file:', error);\n }\n console.log(data);\n});\n```"
4689
+
},
4690
+
{
4691
+
"link": "#how-to-catch-errors-in-callbacks",
4692
+
"title": "How to catch errors in callbacks",
4693
+
"url": "",
4694
+
"level": "intermediate",
4695
+
"theme": "JavaScript, Callbacks, Error Handling",
4696
+
"text": "### How to catch errors in callbacks\n\nWhen working with callbacks, ensure that errors are passed as the first argument to the callback function.\n\n```javascript\nfunction performAsyncOperation(callback) {\n // Simulate error\n const error = new Error('Something went wrong');\n callback(error, null);\n}\n\nperformAsyncOperation((error, result) => {\n if (error) {\n return console.error('Callback error:', error);\n }\n console.log(result);\n});\n```"
"title": "How to use global error handling with try/catch",
4701
+
"url": "",
4702
+
"level": "advanced",
4703
+
"theme": "JavaScript, Error Handling",
4704
+
"text": "### How to use global error handling with try/catch\n\nFor comprehensive error handling, set up global error listeners in your application.\n\n```javascript\nwindow.onerror = function(message, source, lineno, colno, error) {\n console.error('Global error caught:', message, 'at', source + ':' + lineno + ':' + colno);\n};\n\n// Example triggering a global error\nnonExistentFunction();\n```"
"text": "### How to catch errors in Express.js\n\nUse middleware to catch and handle errors in your Express applications.\n\n```javascript\nconst express = require('express');\nconst app = express();\n\n// Your routes here\napp.get('/', (req, res) => {\n throw new Error('Oops!');\n});\n\n// Error handling middleware\napp.use((err, req, res, next) => {\n console.error(err.stack);\n res.status(500).send('Something broke!');\n});\n\napp.listen(3000, () => console.log('Server running on port 3000'));\n```"
4713
+
},
4714
+
{
4715
+
"link": "#how-to-catch-unhandled-rejections",
4716
+
"title": "How to catch unhandled promise rejections",
4717
+
"url": "",
4718
+
"level": "advanced",
4719
+
"theme": "JavaScript, Promises, Error Handling",
4720
+
"text": "### How to catch unhandled promise rejections\n\nListen for unhandled promise rejections to prevent unexpected application crashes.\n\n```javascript\nprocess.on('unhandledRejection', (reason, promise) => {\n console.error('Unhandled Rejection at:', promise, 'reason:', reason);\n});\n\n// Example of an unhandled rejection\nPromise.reject(new Error('Unhandled error'));\n```"
0 commit comments