CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-neomodel

An object mapper for the neo4j graph database.

Pending
Overview
Eval results
Files

exceptions.mddocs/

Exception Handling

Comprehensive exception classes for error handling in neomodel operations. These exceptions provide detailed information about various error conditions that can occur during database operations, model definitions, and data validation.

Capabilities

Base Exceptions

Base exception classes that serve as the foundation for all neomodel-specific errors.

class NeomodelException(Exception):
    """
    Base class that identifies all exceptions raised by neomodel.
    """

class ConstraintValidationFailed(ValueError, NeomodelException):
    """
    Raised when database constraint validation fails.
    
    Args:
        msg (str): Error message describing the constraint violation
    """
    def __init__(self, msg: str): ...

Query and Retrieval Exceptions

Exceptions that occur during node and relationship retrieval operations.

class DoesNotExist(NeomodelException):
    """
    Raised when a requested node or relationship does not exist.
    
    Args:
        msg (str): Error message describing what was not found
    """
    def __init__(self, msg: str): ...

class MultipleNodesReturned(ValueError, NeomodelException):
    """
    Raised when multiple nodes are returned for a query expecting a single result.
    
    Args:
        msg (str): Error message describing the multiple results
    """
    def __init__(self, msg: str): ...

class NotConnected(NeomodelException):
    """
    Raised when nodes are not connected as expected for a relationship operation.
    
    Args:
        action (str): The action that was attempted
        node1: First node in the attempted relationship
        node2: Second node in the attempted relationship
    """
    def __init__(self, action: str, node1, node2): ...

Property Validation Exceptions

Exceptions related to property validation and data type conversion.

class RequiredProperty(NeomodelException):
    """
    Raised when a required property is not provided.
    
    Args:
        key (str): Property name that is required
        cls: Node class that requires the property
    """
    def __init__(self, key: str, cls): ...

class UniqueProperty(ConstraintValidationFailed):
    """
    Raised when a unique constraint violation occurs.
    
    Args:
        msg (str): Error message describing the unique constraint violation
    """
    def __init__(self, msg: str): ...

class InflateError(ValueError, NeomodelException):
    """
    Raised when converting database values to Python objects fails.
    
    Args:
        key (str): Property name causing the error
        cls: Node class being inflated
        msg (str): Error message
        obj (optional): Object that caused the error
    """
    def __init__(self, key: str, cls, msg: str, obj=None): ...

class DeflateError(ValueError, NeomodelException):
    """
    Raised when converting Python objects to database values fails.
    
    Args:
        key (str): Property name causing the error
        cls: Node class being deflated
        msg (str): Error message
        obj: Object that caused the error
    """
    def __init__(self, key: str, cls, msg: str, obj): ...

class InflateConflict(NeomodelException):
    """
    Raised when property inflation results in conflicting values.
    
    Args:
        cls: Node class with conflicting property
        key (str): Property name in conflict
        value: Conflicting value
        nid (str): Node ID with the conflict
    """
    def __init__(self, cls, key: str, value, nid: str): ...

class DeflateConflict(InflateConflict):
    """
    Raised when property deflation results in conflicting values.
    
    Args:
        cls: Node class with conflicting property
        key (str): Property name in conflict
        value: Conflicting value
        nid (str): Node ID with the conflict
    """
    def __init__(self, cls, key: str, value, nid: str): ...

Cardinality Exceptions

Exceptions related to relationship cardinality constraint violations.

class AttemptedCardinalityViolation(NeomodelException):
    """
    Raised when attempting to violate cardinality constraints.
    
    Example: A relationship of type One trying to connect a second node.
    """

class CardinalityViolation(NeomodelException):
    """
    Raised when database state doesn't match cardinality definitions.
    
    Example: A relationship type OneOrMore returns no nodes.
    
    Args:
        rel_manager: Relationship manager with cardinality constraint
        actual: Actual number of relationships found
    """
    def __init__(self, rel_manager, actual): ...

Model Definition Exceptions

Exceptions that occur during model class definition and registration.

class ModelDefinitionException(NeomodelException):
    """
    Base exception for node-to-class registry errors.
    
    Args:
        db_node_rel_class: Database node or relationship class causing the error
        current_node_class_registry (dict): Current node class registry
        current_db_specific_node_class_registry (dict): Database-specific registry
    """
    def __init__(
        self,
        db_node_rel_class,
        current_node_class_registry: dict,
        current_db_specific_node_class_registry: dict,
    ): ...

class NodeClassNotDefined(ModelDefinitionException):
    """
    Raised when unable to resolve a Neo4j Node to a data model object.
    
    This occurs when query results contain nodes with labels that don't
    match any imported class definitions.
    """

class NodeClassAlreadyDefined(ModelDefinitionException):
    """
    Raised when attempting to re-map labels to an already registered class.
    """

class RelationshipClassNotDefined(ModelDefinitionException):
    """
    Raised when unable to resolve a Neo4j Relationship to a data model object.
    """

class RelationshipClassRedefined(ModelDefinitionException):
    """
    Raised when attempting to re-map a relationship label to a different type.
    
    Args:
        db_rel_class_type: Relationship type causing the error
        current_node_class_registry (dict): Current node class registry
        current_db_specific_node_class_registry (dict): Database-specific registry
        remapping_to_class: Class the relationship was attempted to be redefined to
    """
    def __init__(
        self,
        db_rel_class_type,
        current_node_class_registry: dict,
        current_db_specific_node_class_registry: dict,
        remapping_to_class,
    ): ...

Feature Support Exceptions

Exceptions for unsupported features or operations.

class FeatureNotSupported(NeomodelException):
    """
    Raised when a requested feature is not supported.
    
    Args:
        msg (str): Error message describing the unsupported feature
    """
    def __init__(self, msg: str): ...

Usage Examples

Basic Exception Handling

from neomodel import StructuredNode, StringProperty, DoesNotExist, UniqueProperty

class Person(StructuredNode):
    name = StringProperty(unique_index=True, required=True)

# Handle node not found
try:
    person = Person.nodes.get(name="John")
except DoesNotExist:
    print("Person not found")

# Handle unique constraint violation
try:
    Person(name="Alice").save()
    Person(name="Alice").save()  # Will raise UniqueProperty
except UniqueProperty as e:
    print(f"Unique constraint violated: {e.message}")

Cardinality Exception Handling

from neomodel import (
    StructuredNode, StringProperty, RelationshipTo, 
    One, CardinalityViolation
)

class Person(StructuredNode):
    name = StringProperty()
    spouse = RelationshipTo('Person', 'MARRIED_TO', cardinality=One)

person = Person(name="John").save()

# Handle cardinality violation
try:
    spouses = person.spouse.all()  # Will raise if no spouse exists
except CardinalityViolation as e:
    print(f"Cardinality constraint violated: {e}")

Property Validation Exception Handling

from neomodel import (
    StructuredNode, IntegerProperty, 
    RequiredProperty, InflateError
)

class Product(StructuredNode):
    name = StringProperty(required=True)
    price = IntegerProperty()

# Handle required property missing
try:
    Product(price=100).save()  # Missing required 'name'
except RequiredProperty as e:
    print(f"Required property missing: {e.property_name}")

# Handle property inflation error
try:
    # Assuming database contains invalid data for price property
    product = Product.nodes.get(name="Widget")
except InflateError as e:
    print(f"Error inflating property '{e.property_name}': {e.msg}")

Install with Tessl CLI

npx tessl i tessl/pypi-neomodel

docs

database.md

exceptions.md

index.md

nodes.md

properties.md

queries.md

relationships.md

tile.json