Source code for nirfmxbluetooth.errors

"""errors.py - Contains error classes and method(s) to handle error."""

import platform
from typing import Any


def _is_success(code: int) -> bool:
    return code == 0


def _is_error(code: int) -> bool:
    return code < 0


def _is_warning(code: int) -> bool:
    return code > 0


[docs] class Error(Exception): """Base error class for NI-RFmxBT.""" def __init__(self, message): """Base error class for NI-RFmxBT.""" super(Error, self).__init__(message)
[docs] class DriverNotInstalledError(Error): """An error due to using this module without the driver runtime installed.""" def __init__(self): """An error due to using this module without the driver runtime installed.""" super(DriverNotInstalledError, self).__init__("The NI-RFmxBT runtime could not be loaded. Make sure it is installed and its bitness matches that of your Python interpreter. Please visit http://www.ni.com/downloads/drivers/ to download and install it.") # type: ignore
[docs] class DriverTooOldError(Error): """An error due to using this module with an older version of the NI-RFmxBT driver runtime.""" def __init__(self): """An error due to using this module with an older version of the NI-RFmxBT driver runtime.""" super(DriverTooOldError, self).__init__("A function was not found in the NI-RFmxBT runtime. Please visit http://www.ni.com/downloads/drivers/ to download a newer version and install it.") # type: ignore
[docs] class DriverTooNewError(Error): """An error due to the NI-RFmxBT driver runtime being too new for this module.""" def __init__(self): """An error due to the NI-RFmxBT driver runtime being too new for this module.""" super(DriverTooNewError, self).__init__("The NI-RFmxBT runtime returned an unexpected value. This can occur if it is too new for the nirfmxbluetooth Python module. Upgrade the nirfmxbluetooth Python module.") # type: ignore
[docs] class UnsupportedConfigurationError(Error): """An error due to using this module in an usupported platform.""" def __init__(self): """An error due to using this module in an usupported platform.""" super(UnsupportedConfigurationError, self).__init__("System configuration is unsupported: " + platform.architecture()[0] + " " + platform.system()) # type: ignore
[docs] class RpcError(Error): """An error specific to sessions to the NI gRPC Device Server.""" def __init__(self, rpc_code, description): """An error specific to sessions to the NI gRPC Device Server.""" self.rpc_code = rpc_code self.description = description try: import grpc rpc_error = str(grpc.StatusCode(self.rpc_code)) except Exception: rpc_error = str(self.rpc_code) super(RpcError, self).__init__(rpc_error + ": " + self.description) # type: ignore
[docs] def handle_error( library_interpreter: Any, code: int, ignore_warnings: bool, is_error_handling: bool ) -> None: r"""Helper function for handling errors returned by NI-RFmxBT Library. It calls back into the LibraryInterpreter to get the corresponding error description and raises if necessary. """ if _is_success(code) or (_is_warning(code) and ignore_warnings): return if is_error_handling: # The caller is in the midst of error handling and an error occurred. # Don't try to get the description or we'll start recursing until the stack overflows. description = "" else: description = library_interpreter.get_error_description(code) if _is_error(code): import nirfmxinstr raise nirfmxinstr.RFmxError(code, description) # type: ignore