Skip to content

Enhance error handling with structured exception classes #3270

@bkidd1

Description

@bkidd1

🚀 The feature

Currently, mem0 uses basic APIError exceptions that provide limited context for debugging and user feedback. It's difficult to implement proper error recovery and provide meaningful feedback. Btw I'm building MindMe, a wellness conversational agent.

Would be helpful to implement structured exception classes with error codes, suggestions, and debug information:

class MemoryError(Exception):
    """Base exception for all memory-related errors"""
    
    def __init__(self, message: str, error_code: str, details: dict = None, 
                 suggestion: str = None, debug_info: dict = None):
        self.message = message
        self.error_code = error_code
        self.details = details or {}
        self.suggestion = suggestion
        self.debug_info = debug_info or {}
        super().__init__(self.message)

class AuthenticationError(MemoryError):
    """Raised when authentication fails"""
    pass

class RateLimitError(MemoryError):
    """Raised when rate limits are exceeded"""
    pass

class ValidationError(MemoryError):
    """Raised when input validation fails"""
    pass

class MemoryNotFoundError(MemoryError):
    """Raised when a memory is not found"""
    pass

class NetworkError(MemoryError):
    """Raised when network connectivity issues occur"""
    pass

class ConfigurationError(MemoryError):
    """Raised when client configuration is invalid"""
    pass

class MemoryQuotaExceededError(MemoryError):
    """Raised when user's memory quota is exceeded"""
    pass

class MemoryCorruptionError(MemoryError):
    """Raised when memory data is corrupted"""
    pass

class VectorSearchError(MemoryError):
    """Raised when vector search operations fail"""
    pass

class CacheError(MemoryError):
    """Raised when caching operations fail"""
    pass

ex:

try:
    memory.add(content, user_id=user_id)
except RateLimitError as e:
    # Implement exponential backoff
    time.sleep(e.debug_info.get('retry_after', 60))
except MemoryQuotaExceededError as e:
    # Trigger quota upgrade flow
    logger.error(f"Quota exceeded: {e.error_code}")
except ValidationError as e:
    # Return user-friendly error
    raise HTTPException(400, detail=e.suggestion)

Lmk what you think, I'd love to work on this

Motivation, pitch

Currently, mem0 uses basic APIError exceptions that provide limited context for debugging and user feedback. It's difficult to implement proper error recovery and provide meaningful feedback. Btw I'm building MindMe, a wellness conversational agent.

Would be helpful to implement structured exception classes with error codes, suggestions, and debug information. Something like this would also help make sentry more useful for my project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions