Skip to content

Refactor exception message handling with custom exception classes #324

Open
@coderabbitai

Description

@coderabbitai

Background

In PR #323, CodeRabbit suggested refactoring exception message handling by introducing custom exception classes to address Ruff TRY003 warnings about long messages outside exception classes.

Current Approach

Currently, the codebase uses direct exception raising with detailed error messages that include CWE references, such as:

except ImportError as baton:
    raise ImportError("[CWE-440] Unable to import sys module.") from baton

Proposed Solution

Implement custom exception classes to encapsulate these messages for better maintainability:

class ImportFailureError(ImportError):
    """Custom exception for import failures with CWE references."""
    def __init__(self, component, cwe="440", *args, **kwargs):
        self.cwe = cwe
        message = f"[CWE-{cwe}] Unable to import {component}."
        super().__init__(message, *args, **kwargs)

# Usage example:
except ImportError as baton:
    raise ImportFailureError("sys module", "440") from baton

Benefits

  1. Improved code maintainability
  2. Consistent error messaging
  3. Addresses Ruff TRY003 linting warnings
  4. Better encapsulation of error information

References

Metadata

Metadata

Labels

MulticastAny main project file changesPython LangChanges to Python source codeenhancementNew feature or requestquestionFurther information is requested

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions