Utilizing Consistent Error Handling Techniques

Learn various strategies for handling errors consistently across your JavaScript applications to improve user experience and maintainability.

0 likes
42 views

Rule Content

{
  "title": "Utilizing Consistent Error Handling Techniques",
  "description": "Learn various strategies for handling errors consistently across your JavaScript applications to improve user experience and maintainability.",
  "category": "JavaScript Cursor Rules",
  "rules": [
    {
      "pattern": "try\\s*\\{[^\\}]*\\}\\s*catch\\s*\\(\\s*\\w+\\s*\\)\\s*\\{[^\\}]*\\}",
      "message": "Ensure that all try-catch blocks include meaningful error messages and appropriate error handling logic.",
      "severity": "warning"
    },
    {
      "pattern": "throw\\s+new\\s+Error\\(.*\\)",
      "message": "Consider using custom error classes to provide more context and facilitate better error handling.",
      "severity": "info"
    },
    {
      "pattern": "process\\.on\\(\\s*['\"]uncaughtException['\"]\\s*,\\s*function\\s*\\(\\s*\\w+\\s*\\)\\s*\\{[^\\}]*\\}\\s*\\)",
      "message": "Implement global error handlers to catch unhandled exceptions and prevent application crashes.",
      "severity": "warning"
    },
    {
      "pattern": "window\\.onerror\\s*=\\s*function\\s*\\(.*\\)\\s*\\{[^\\}]*\\}\\s*;",
      "message": "Use window.onerror to handle global errors in the browser and provide user-friendly feedback.",
      "severity": "info"
    },
    {
      "pattern": "fetch\\s*\\(.*\\)\\.then\\s*\\(.*\\)\\.catch\\s*\\(.*\\)",
      "message": "Ensure that all fetch requests include a catch method to handle potential errors.",
      "severity": "warning"
    },
    {
      "pattern": "async\\s+function\\s+\\w+\\s*\\(.*\\)\\s*\\{[^\\}]*\\}",
      "message": "Wrap the contents of async functions in try-catch blocks to handle asynchronous errors effectively.",
      "severity": "warning"
    },
    {
      "pattern": "logger\\.error\\s*\\(.*\\)",
      "message": "Use a dedicated logging service to capture and monitor errors for better debugging and maintenance.",
      "severity": "info"
    }
  ]
}