Errors

Exceptions

This page documents the custom exception types used throughout PyTestLab. Understanding these exceptions is essential for writing robust, reliable, and safe test scripts.


Exception Reference

pytestlab.errors.InstrumentConnectionError(instrument=None, message='')

Bases: Exception

Exception raised for SCPI instrument connection errors.

Source code in pytestlab/errors.py
def __init__(self, instrument: str | None = None, message: str = "") -> None:
    self.instrument = instrument
    self.message = message
    if instrument:
        super().__init__(f"Failed to connect to instrument '{instrument}'. {message}")
    else:
        super().__init__(f"Failed to connect to instrument. {message}")

Attributes

instrument = instrument instance-attribute

message = message instance-attribute

Functions

pytestlab.errors.InstrumentCommunicationError(instrument=None, command=None, message='')

Bases: Exception

Exception raised for SCPI communication errors.

Source code in pytestlab/errors.py
def __init__(
    self, instrument: str | None = None, command: str | None = None, message: str = ""
) -> None:
    self.instrument = instrument
    self.command = command
    self.message = message
    full_message = f"Error in SCPI communication with instrument '{instrument}'"
    if command:
        full_message += f" while sending command '{command}'"
    full_message += f". {message}"
    super().__init__(full_message)

Attributes

command = command instance-attribute

instrument = instrument instance-attribute

message = message instance-attribute

Functions

pytestlab.errors.InstrumentParameterError(parameter=None, value=None, valid_range=None, message='')

Bases: ValueError

Exception raised for invalid parameters given to an instrument.

Source code in pytestlab/errors.py
def __init__(self, parameter=None, value=None, valid_range=None, message=""):
    self.parameter = parameter
    self.value = value
    self.valid_range = valid_range
    self.message = message
    full_message = "Invalid parameter value for instrument"
    if parameter:
        full_message += f" for parameter '{parameter}'"
    if value is not None:
        full_message += f": received '{value}'"
    if valid_range:
        full_message += f", but expected a value in the range {valid_range}"
    full_message += f". {message}"
    super().__init__(full_message)

Attributes

message = message instance-attribute

parameter = parameter instance-attribute

valid_range = valid_range instance-attribute

value = value instance-attribute

Functions

pytestlab.errors.InstrumentConfigurationError(instrument=None, message='')

Bases: Exception

Exception raised for instrument configuration errors.

Source code in pytestlab/errors.py
def __init__(self, instrument=None, message=""):
    self.instrument = instrument
    self.message = message
    if instrument:
        super().__init__(f"Invalid configuration for instrument '{instrument}'. {message}")
    else:
        super().__init__(f"Invalid instrument configuration. {message}")

Attributes

instrument = instrument instance-attribute

message = message instance-attribute

Functions

pytestlab.errors.DatabaseError(operation=None, message='')

Bases: Exception

Exception raised for database errors.

Source code in pytestlab/errors.py
def __init__(self, operation=None, message=""):
    self.operation = operation
    self.message = message
    if operation:
        super().__init__(f"Error in database operation '{operation}'. {message}")
    else:
        super().__init__(f"Error in database operation. {message}")

Attributes

message = message instance-attribute

operation = operation instance-attribute

Functions

pytestlab.errors.InstrumentNotFoundError(name)

Bases: Exception

For instrument not found errors.

Source code in pytestlab/errors.py
def __init__(self, name):
    super().__init__(f"Instrument {name} not found in the manager's collection.")

Functions

ReplayMismatchError

pytestlab.errors.ReplayMismatchError(message, instrument=None, command=None, expected_command=None, actual_command=None, log_index=None)

Bases: Exception

Raised when a command during replay does not match the recorded log.

Source code in pytestlab/errors.py
def __init__(
    self,
    message,
    instrument=None,
    command=None,
    expected_command=None,
    actual_command=None,
    log_index=None,
):
    # Store additional attributes
    self.instrument = instrument
    self.command = command
    self.expected_command = expected_command
    self.actual_command = actual_command
    self.log_index = log_index

    # Use the message directly without parent class formatting
    super().__init__(message)

Attributes

actual_command = actual_command instance-attribute

command = command instance-attribute

expected_command = expected_command instance-attribute

instrument = instrument instance-attribute

log_index = log_index instance-attribute

Functions


Usage Example

from pytestlab.errors import (
    InstrumentConnectionError,
    InstrumentParameterError,
    DatabaseError,
)

q
except InstrumentParameterError as e:
    print(f"Invalid parameter: {e}")
except DatabaseError as e:
    print(f"Database error: {e}")

For a practical guide to error handling, see the Error Handling Guide.