Error Handling Patterns in Node.js Applications
Understand different approaches to implement robust error handling mechanisms in your Node.js applications.
0 likes
3 views
Rule Content
{ "title": "Error Handling Patterns in Node.js Applications", "description": "Understand different approaches to implement robust error handling mechanisms in your Node.js applications.", "category": "Node.js Cursor Rules", "rules": [ { "pattern": "try\\s*\\{[^\\}]*\\}\\s*catch\\s*\\(\\s*\\w+\\s*\\)\\s*\\{[^\\}]*\\}", "message": "Ensure that all synchronous code is wrapped in try-catch blocks to handle exceptions gracefully.", "severity": "warning" }, { "pattern": "\\.then\\(.*?\\)\\s*\\.catch\\(.*?\\)", "message": "Verify that all Promises have a .catch() handler to manage rejections properly.", "severity": "warning" }, { "pattern": "process\\.on\\(\\s*['\"]uncaughtException['\"]\\s*,\\s*function\\s*\\(\\s*err\\s*\\)\\s*\\{[^\\}]*\\}\\s*\\)", "message": "Implement a process.on('uncaughtException') handler to catch and log uncaught exceptions.", "severity": "warning" }, { "pattern": "process\\.on\\(\\s*['\"]unhandledRejection['\"]\\s*,\\s*function\\s*\\(\\s*reason\\s*\\)\\s*\\{[^\\}]*\\}\\s*\\)", "message": "Implement a process.on('unhandledRejection') handler to catch and log unhandled promise rejections.", "severity": "warning" }, { "pattern": "class\\s+\\w+\\s+extends\\s+Error\\s*\\{[^\\}]*\\}", "message": "Define custom error classes by extending the built-in Error object to create application-specific error types.", "severity": "info" }, { "pattern": "app\\.use\\(\\s*function\\s*\\(\\s*err\\s*,\\s*req\\s*,\\s*res\\s*,\\s*next\\s*\\)\\s*\\{[^\\}]*\\}\\s*\\)", "message": "Implement centralized error-handling middleware in Express to manage errors consistently across the application.", "severity": "info" }, { "pattern": "logger\\.error\\(.*?\\)", "message": "Ensure that all errors are logged using a mature logging library like Winston or Pino for better error visibility.", "severity": "info" }, { "pattern": "eventEmitter\\.on\\(\\s*['\"]error['\"]\\s*,\\s*function\\s*\\(\\s*err\\s*\\)\\s*\\{[^\\}]*\\}\\s*\\)", "message": "Subscribe to the 'error' event of Event Emitters and Streams to handle exceptions in event-driven code.", "severity": "warning" }, { "pattern": "return\\s+await\\s+\\w+\\(.*?\\)", "message": "Always 'return await' when returning a promise within an async function to ensure the full stack trace is preserved in case of an error.", "severity": "info" }, { "pattern": "app\\.post\\(.*?\\[.*?check\\(.*?\\)\\.is.*?\\]\\s*,\\s*function\\s*\\(\\s*req\\s*,\\s*res\\s*,\\s*next\\s*\\)\\s*\\{[^\\}]*\\}\\s*\\)", "message": "Validate user input using libraries like express-validator to prevent errors from invalid data.", "severity": "info" } ] }