Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change: Add error context handling #113

Open
mathysgrapotte opened this issue Feb 19, 2025 · 1 comment
Open

change: Add error context handling #113

mathysgrapotte opened this issue Feb 19, 2025 · 1 comment

Comments

@mathysgrapotte
Copy link
Owner

Is your change request related to a problem? Please describe.

Current errors lack operational context needed to debug pipeline failures.

Describe the solution you'd like

  1. Add BaseComponent._error() helper method
  2. Implement context managers for file operations
  3. Enforce error context in data handlers/loaders including:
    • Component name
    • Config snapshot
    • Input metadata
@mathysgrapotte
Copy link
Owner Author

example implementation for context :

from typing import Dict, Any, Type
from stimulus.exceptions import StimulusError

class BaseComponent:
    """Base class providing error context handling"""
    
    def __init__(self, config: Dict[str, Any]):
        self.config = config
        
    def _component_context(self) -> Dict[str, Any]:
        return {
            "component": self.__class__.__name__,
            "config_keys": list(self.config.keys()),
            "phase": getattr(self, "current_phase", "initialization")
        }
    
    def error(self, exc_type: Type[StimulusError], msg: str, **ctx) -> StimulusError:
        return exc_type(msg).add_context(
            **self._component_context(),
            **ctx
        )

# Usage in data handler
class ImageHandler(BaseComponent):
    def load(self, path: str):
        if not path.endswith(".png"):
            raise self.error(
                StimulusError,
                "Unsupported image format",
                allowed_formats=[".png", ".jpg"],
                requested_file=path
            )

@mathysgrapotte mathysgrapotte moved this to Todo - mid issues in Stimulus v1.0 Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

1 participant