Skip to content

Exceptions

Custom exception classes for error handling.

Base Exception

pycentral_error

PycentralError(*args)

Bases: Exception

Base exception class for all pycentral-specific errors.

This exception serves as the base class for all custom exceptions in the pycentral library. It provides common functionality for error handling and message formatting.

Attributes:

Name Type Description
base_msg str

The base error message for this exception type.

message str

The complete formatted error message.

response dict

The API response associated with the error, if applicable.

Example
>>> raise PycentralError("An unexpected error occurred")
PycentralError: 'PYCENTRAL ERROR, An unexpected error occurred'
Source code in pycentral/exceptions/pycentral_error.py
def __init__(self, *args):
    self.message = ", ".join(
        (
            self.base_msg,
            *(str(a) for a in args),
        )
    )
    self.response = None

Login Error

login_error

LoginError(message, status_code=None, *details)

Bases: PycentralError

Exception raised when login or authentication fails.

This exception is raised when authentication to Central fails, typically due to invalid credentials, expired tokens, or network issues.

Attributes:

Name Type Description
base_msg str

The base error message for this exception type.

message str

Detailed error message describing the login failure.

status_code int

HTTP status code associated with the login failure, if available.

Example
>>> raise LoginError(msg, status_code)
LoginError: LOGIN ERROR - Invalid client or client credentials. for
new_central. Provide valid client_id and client_secret to create an
access token. (status_code=401)
Source code in pycentral/exceptions/login_error.py
def __init__(self, message, status_code=None, *details):
    self.status_code = status_code

    parts = [self.base_msg]
    if message:
        parts.append(str(message))
    if details:
        parts.extend(str(d) for d in details)

    self.message = " - ".join(parts)

Response Error

response_error

ResponseError(*args)

Bases: PycentralError

Exception raised when an API response indicates an error.

This exception is raised when the API returns an error response, such as HTTP error codes (4xx, 5xx) or when the response content indicates a failure.

Attributes:

Name Type Description
base_msg str

The base error message for this exception type.

message str

Detailed error message describing the response failure.

response dict

The API response object containing error details.

Example
>>> raise ResponseError({"code": 404, "msg": "Not found"}, "Resource does not exist")
ResponseError: 'RESPONSE ERROR: Resource does not exist: Response: {"code": 404, "msg": "Not found"}'
Source code in pycentral/exceptions/response_error.py
def __init__(self, *args):
    self.message = None
    self.response = None
    if args:
        self.response = args[0]
        if len(args) > 1:
            self.message = ", ".join(str(a) for a in args[1:])
    else:
        self.message = None

Parameter Error

parameter_error

ParameterError(*args)

Bases: VerificationError

Exception raised when invalid parameters are passed to functions.

This exception is a subclass of VerificationError and is used to indicate that one or more parameters provided to a function are invalid, missing, or do not meet the required constraints.

Attributes:

Name Type Description
base_msg str

The base error message for this exception type.

Example
>>> raise ParameterError(f"name must be a valid string found {type(name)}")
ParameterError: "PARAMETER ERROR: name must be a valid string found <class 'int'>"
Source code in pycentral/exceptions/verification_error.py
def __init__(self, *args):
    self.message = None
    self.module = None
    if args:
        self.module = args[0]
        if len(args) > 1:
            self.message = ", ".join(str(a) for a in args[1:])

Verification Error

verification_error

VerificationError(*args)

Bases: PycentralError

Exception raised when verification checks fail during pycentral operations.

This exception is raised when verification checks of values fail prior to API execution. It serves as a base class for more specific verification-related exceptions.

Attributes:

Name Type Description
base_msg str

The base error message for this exception type.

message str

Detailed error message describing the verification failure.

module str

The module or context where the verification error occurred.

Example
>>> raise VerificationError(err_str, " get_resource_str() failed")
VerificationError: "VERIFICATION ERROR: Missing self.object_data['resource'] attribute DETAIL: get_resource_str() failed"
Source code in pycentral/exceptions/verification_error.py
def __init__(self, *args):
    self.message = None
    self.module = None
    if args:
        self.module = args[0]
        if len(args) > 1:
            self.message = ", ".join(str(a) for a in args[1:])