Skip to content

Errors

Exception classes raised by grpcvcr.

Overview

All grpcvcr exceptions inherit from GrpcvcrError, making it easy to catch any grpcvcr-specific error.

from grpcvcr import (
    CassetteNotFoundError,
    GrpcvcrError,
    RecordingDisabledError,
    RecordMode,
    recorded_channel,
)

try:
    with recorded_channel(
        "missing.yaml", "localhost:50051", record_mode=RecordMode.NONE
    ) as channel:
        ...
except CassetteNotFoundError:
    print("Cassette file not found")
except RecordingDisabledError:
    print("No matching interaction and recording is disabled")
except GrpcvcrError:
    print("Some other grpcvcr error")

API Reference

Bases: Exception

Base exception for all grpcvcr errors.

All grpcvcr exceptions inherit from this class, making it easy to catch any grpcvcr-specific error.

Example
try:
    with recorded_channel("test.yaml", "localhost:50051") as channel:
        stub = MyServiceStub(channel)
        response = stub.GetUser(request)
except GrpcvcrError as e:
    print(f"grpcvcr error: {e}")

Bases: GrpcvcrError

Raised when a cassette file cannot be found.

This occurs when using RecordMode.NONE or RecordMode.ONCE (after initial recording) and the cassette file doesn't exist.

Source code in src/grpcvcr/errors.py
def __init__(self, path: str) -> None:
    self.path = path
    """Path to the missing cassette file."""
    super().__init__(f"Cassette not found: {path}")

path instance-attribute

path = path

Path to the missing cassette file.

Bases: GrpcvcrError

Raised when a cassette cannot be written to disk.

This can occur due to permission issues, disk full, or other I/O errors.

Source code in src/grpcvcr/errors.py
def __init__(self, path: str, cause: Exception) -> None:
    self.path = path
    """Path where the cassette was being written."""

    self.cause = cause
    """The underlying exception that caused the write failure."""
    super().__init__(f"Failed to write cassette {path}: {cause}")

path instance-attribute

path = path

Path where the cassette was being written.

cause instance-attribute

cause = cause

The underlying exception that caused the write failure.

Bases: GrpcvcrError

Raised when no recorded interaction matches the request.

This typically means the test is making a new RPC call that wasn't recorded in the cassette.

Source code in src/grpcvcr/errors.py
def __init__(
    self,
    method: str,
    request: bytes,
    available: list[Interaction],
) -> None:
    self.method = method
    """The gRPC method that was called."""

    self.request = request
    """The serialized request bytes."""

    self.available = available
    """List of available recorded interactions."""

    available_methods = [i.method for i in available]
    super().__init__(f"No matching interaction for {method}. Available: {available_methods}")

method instance-attribute

method = method

The gRPC method that was called.

request instance-attribute

request = request

The serialized request bytes.

available instance-attribute

available = available

List of available recorded interactions.

Bases: GrpcvcrError

Raised when recording is attempted but disabled.

This occurs in RecordMode.NONE when a request is made that doesn't match any recorded interaction.

Source code in src/grpcvcr/errors.py
def __init__(self, method: str) -> None:
    self.method = method
    """The gRPC method that was called."""
    super().__init__(f"Recording disabled but no matching interaction for: {method}")

method instance-attribute

method = method

The gRPC method that was called.

Bases: GrpcvcrError

Raised when request/response serialization fails.

This can occur when parsing a malformed cassette file or when serializing data to save.

Source code in src/grpcvcr/errors.py
def __init__(self, message: str, cause: Exception | None = None) -> None:
    self.cause = cause
    """The underlying exception, if any."""
    super().__init__(message)

cause instance-attribute

cause = cause

The underlying exception, if any.