Source code for brainaccess.utils.exceptions

"""Exceptions raised by the BrainAccess Python package."""

import ctypes
import enum
import warnings


[docs] class BrainAccessException(Exception): """Raised when a BrainAccess operation fails. Covers connection, Bluetooth, headset and configuration failures as well as invalid arguments. The exception message says what went wrong. """
def _callback() -> tuple: success: bool = False @ctypes.CFUNCTYPE(None, ctypes.c_bool, ctypes.c_void_p) def callback(_success: bool, user_data: ctypes.c_void_p) -> None: nonlocal success success = bool(_success) def get_success() -> bool: return success return callback, get_success class _Error(enum.Enum): OK = 0 CONNECTION = 1 UNSUPPORTED_DEVICE = 2 WRONG_VALUE = 3 BLUETOOTH_DISABLED = 4 BLUETOOTH_ADAPTER_NOT_FOUND = 5 ADAPTER_OUT_OF_INDEX = 6 UPDATE_FILE_NOT_FOUND = 7 UPDATE_INITIATED_UNSUCCESSFULLY = 8 UPDATE_FAILED_DEVICE_DISCONNECTED = 9 ANNOTATION_UNAVAILABLE_CALIBRATING = 10 NO_DEVICES_FOUND = 11 NOT_ALL_MANAGERS_CLOSED = 12 CORE_NOT_INITIALIZED = 13 PATH_NOT_FOUND = 14 BLUETOOTH_SERVICE_UNAVAILABLE = 15 UPDATE_REJECTED = 16 STREAM_START_FAILED = 17 UNKNOWN = 0xFF def _get_error(val) -> _Error: try: return _Error(val) except ValueError: return _Error.UNKNOWN def _handle_error(val: int) -> bool: err = _get_error(val) if err == _Error.OK: return True elif err == _Error.CONNECTION: raise BrainAccessException("Connection error; check that the headset is on and in range") elif err == _Error.UNSUPPORTED_DEVICE: raise BrainAccessException("Unsupported device; it is not a supported BrainAccess headset") elif err == _Error.WRONG_VALUE: raise BrainAccessException("Invalid argument value") elif err == _Error.BLUETOOTH_DISABLED: raise BrainAccessException("Bluetooth is disabled; turn Bluetooth on and try again") elif err == _Error.BLUETOOTH_ADAPTER_NOT_FOUND: raise BrainAccessException("Bluetooth adapter not found; check that the computer has a Bluetooth adapter") elif err == _Error.ADAPTER_OUT_OF_INDEX: raise BrainAccessException("Bluetooth adapter index is out of range") elif err == _Error.UPDATE_FILE_NOT_FOUND: raise BrainAccessException("Update file not found") elif err == _Error.UPDATE_INITIATED_UNSUCCESSFULLY: raise BrainAccessException("Update initiated unsuccessfully") elif err == _Error.UPDATE_FAILED_DEVICE_DISCONNECTED: raise BrainAccessException( "Update failed: device disconnected or reported no result within 10 s" ) elif err == _Error.ANNOTATION_UNAVAILABLE_CALIBRATING: warnings.warn("Annotation unavailable while calibrating", stacklevel=3) return False elif err == _Error.NO_DEVICES_FOUND: raise BrainAccessException("No devices found; check that the headset is on and in range") elif err == _Error.NOT_ALL_MANAGERS_CLOSED: raise BrainAccessException("Destroy all EEG managers before closing BrainAccess Core") elif err == _Error.CORE_NOT_INITIALIZED: raise BrainAccessException("BrainAccess Core is not initialized; call brainaccess.core.init() first") elif err == _Error.PATH_NOT_FOUND: raise BrainAccessException("Path not found; check that the file exists and the path is correct") elif err == _Error.BLUETOOTH_SERVICE_UNAVAILABLE: raise BrainAccessException( "Bluetooth service unavailable or not responding; check that Bluetooth is running" ) elif err == _Error.UPDATE_REJECTED: raise BrainAccessException( "Device rejected the update image; it keeps its current firmware" ) elif err == _Error.STREAM_START_FAILED: raise BrainAccessException( "Device did not start the stream: its EEG converter failed, or it refused " "the start after rejecting the configuration; retry" ) else: raise BrainAccessException("Unknown error from BrainAccess Core") class _ErrorBacore(enum.Enum): OK = 0 CONFIG_TYPE = 1 WRONG_ADAPTER_VALUE = 2 INCOMPATIBLE_VERSION = 3 NOT_ENABLED = 4 NOT_FOUND = 5 CONFIG_PARSE = 6 INIT_ALREADY_INITIALIZED = 7 INVALID_FILE_PATH = 8 INIT_NOT_INITIALIZED = 9 INIT_NOT_ALL_MANAGERS_CLOSED = 12 UNKNOWN = 0xFF def _get_error_bacore(val) -> _ErrorBacore: try: return _ErrorBacore(val) except ValueError: return _ErrorBacore.UNKNOWN def _handle_error_bacore(val: int) -> bool: err = _get_error_bacore(val) if err == _ErrorBacore.OK: return True elif err == _ErrorBacore.CONFIG_TYPE: raise BrainAccessException( "Configuration file contains a value of the wrong type" ) elif err == _ErrorBacore.WRONG_ADAPTER_VALUE: raise BrainAccessException("Invalid Bluetooth adapter index in the configuration") elif err == _ErrorBacore.INCOMPATIBLE_VERSION: raise BrainAccessException("The configuration is incompatible with this version of BrainAccess Core") elif err == _ErrorBacore.NOT_ENABLED: raise BrainAccessException("Bluetooth is not enabled; turn Bluetooth on and try again") elif err == _ErrorBacore.NOT_FOUND: raise BrainAccessException("Bluetooth adapter not found; check that the computer has a Bluetooth adapter") elif err == _ErrorBacore.CONFIG_PARSE: raise BrainAccessException("Error parsing configuration file") elif err == _ErrorBacore.INIT_ALREADY_INITIALIZED: raise BrainAccessException("BrainAccess Core is already initialized") elif err == _ErrorBacore.INVALID_FILE_PATH: raise BrainAccessException("Invalid file path; check that the file exists and the path is correct") elif err == _ErrorBacore.INIT_NOT_INITIALIZED: raise BrainAccessException("BrainAccess Core is not initialized; call brainaccess.core.init() first") else: raise BrainAccessException("Unknown error from BrainAccess Core")