Skip to content

Exceptions

All library exceptions derive from MregApiBaseError. API errors (APIError and subclasses) carry the originating HTTP response and expose parsed error details.

exceptions

Exception classes for MREG API errors.

APIError

Bases: MregApiBaseError

Exception class for API errors.

Parses drf-standardized-errors errors from the MREG API if present in response.

detail cached property

detail: str

Clean human-readable error message from the response.

Joins the detail of each parsed error with "; ". Empty if the response has no parseable errors.

details property

details: str

Get the error details from the response.

details_json property

details_json: str

Get the error details from the response.

errors cached property

Get the parsed MREG errors from the response.

Returns:

Type Description
MREGErrorResponse

The MREGErrorResponse object, or a default MREGErrorResponse with type "unknown" if the response

request property

request: Request | None

Get the request that triggered the exception.

Uses the request from the cause if not set directly.

Returns:

Type Description
Request | None

The request object or None if not set.

response property

response: Response | None

Get the response object associated with the error.

Uses the response from the cause if not set directly.

Returns:

Type Description
Response | None

The response object or None if not set.

status_code property

status_code: int | None

Get the HTTP status code of the response, if available.

__init__

__init__(
    message: str = "", response: Response | None = None
)

Initialize an APIError exception.

Parameters:

Name Type Description Default
message str

An optional message override. When empty, the message is derived from the response (parsed error details, then raw text, then reason phrase) via formatted_message.

''
response Response | None

The response object that triggered the exception.

None

__str__

__str__() -> str

Return the full formatted error message (request info + details).

formatted_message

formatted_message(*, json: bool = False) -> str

Get a formatted error message including error details.

Parameters:

Name Type Description Default
json bool

Whether to include error details as JSON. Defaults to False.

False

Returns:

Name Type Description
str str

The formatted error message.

CacheError

Bases: MregApiBaseError

Error related to caching.

CacheMiss

Bases: CacheError

Cache miss error.

DeleteError

Bases: APIError

Error class for failed deletion.

detail cached property

detail: str

Clean human-readable error message from the response.

Joins the detail of each parsed error with "; ". Empty if the response has no parseable errors.

details property

details: str

Get the error details from the response.

details_json property

details_json: str

Get the error details from the response.

errors cached property

Get the parsed MREG errors from the response.

Returns:

Type Description
MREGErrorResponse

The MREGErrorResponse object, or a default MREGErrorResponse with type "unknown" if the response

request property

request: Request | None

Get the request that triggered the exception.

Uses the request from the cause if not set directly.

Returns:

Type Description
Request | None

The request object or None if not set.

response property

response: Response | None

Get the response object associated with the error.

Uses the response from the cause if not set directly.

Returns:

Type Description
Response | None

The response object or None if not set.

status_code property

status_code: int | None

Get the HTTP status code of the response, if available.

__init__

__init__(
    message: str = "", response: Response | None = None
)

Initialize an APIError exception.

Parameters:

Name Type Description Default
message str

An optional message override. When empty, the message is derived from the response (parsed error details, then raw text, then reason phrase) via formatted_message.

''
response Response | None

The response object that triggered the exception.

None

__str__

__str__() -> str

Return the full formatted error message (request info + details).

formatted_message

formatted_message(*, json: bool = False) -> str

Get a formatted error message including error details.

Parameters:

Name Type Description Default
json bool

Whether to include error details as JSON. Defaults to False.

False

Returns:

Name Type Description
str str

The formatted error message.

EntityAlreadyExists

Bases: MregApiBaseError

Entity already exists when none was expected.

EntityNotFound

Bases: MregApiBaseError

No entity found when at least one was expected.

EntityOwnershipMismatch

Bases: MregApiBaseError

Entity already exists but is owned by someone else.

ForceMissing

Bases: MregApiBaseError

Error class for missing force flag.

GetError

Bases: APIError

Error class for failed retrieval.

detail cached property

detail: str

Clean human-readable error message from the response.

Joins the detail of each parsed error with "; ". Empty if the response has no parseable errors.

details property

details: str

Get the error details from the response.

details_json property

details_json: str

Get the error details from the response.

errors cached property

Get the parsed MREG errors from the response.

Returns:

Type Description
MREGErrorResponse

The MREGErrorResponse object, or a default MREGErrorResponse with type "unknown" if the response

request property

request: Request | None

Get the request that triggered the exception.

Uses the request from the cause if not set directly.

Returns:

Type Description
Request | None

The request object or None if not set.

response property

response: Response | None

Get the response object associated with the error.

Uses the response from the cause if not set directly.

Returns:

Type Description
Response | None

The response object or None if not set.

status_code property

status_code: int | None

Get the HTTP status code of the response, if available.

__init__

__init__(
    message: str = "", response: Response | None = None
)

Initialize an APIError exception.

Parameters:

Name Type Description Default
message str

An optional message override. When empty, the message is derived from the response (parsed error details, then raw text, then reason phrase) via formatted_message.

''
response Response | None

The response object that triggered the exception.

None

__str__

__str__() -> str

Return the full formatted error message (request info + details).

formatted_message

formatted_message(*, json: bool = False) -> str

Get a formatted error message including error details.

Parameters:

Name Type Description Default
json bool

Whether to include error details as JSON. Defaults to False.

False

Returns:

Name Type Description
str str

The formatted error message.

IPNetworkError

Bases: ValueError, MregApiBaseError

Error class for IP network/address errors.

InputFailure

Bases: MregApiBaseError, ValueError

Error class for input failure.

InternalError

Bases: MregApiBaseError

Error class for internal errors.

InvalidAuthTokenError

Bases: LoginFailedError

Invalid authentication token.

detail cached property

detail: str

Clean human-readable error message from the response.

Joins the detail of each parsed error with "; ". Empty if the response has no parseable errors.

details property

details: str

Get the error details from the response.

details_json property

details_json: str

Get the error details from the response.

errors cached property

Get the parsed MREG errors from the response.

Returns:

Type Description
MREGErrorResponse

The MREGErrorResponse object, or a default MREGErrorResponse with type "unknown" if the response

request property

request: Request | None

Get the request that triggered the exception.

Uses the request from the cause if not set directly.

Returns:

Type Description
Request | None

The request object or None if not set.

response property

response: Response | None

Get the response object associated with the error.

Uses the response from the cause if not set directly.

Returns:

Type Description
Response | None

The response object or None if not set.

status_code property

status_code: int | None

Get the HTTP status code of the response, if available.

__init__

__init__(
    message: str = "", response: Response | None = None
)

Initialize an APIError exception.

Parameters:

Name Type Description Default
message str

An optional message override. When empty, the message is derived from the response (parsed error details, then raw text, then reason phrase) via formatted_message.

''
response Response | None

The response object that triggered the exception.

None

__str__

__str__() -> str

Return the full formatted error message (request info + details).

formatted_message

formatted_message(*, json: bool = False) -> str

Get a formatted error message including error details.

Parameters:

Name Type Description Default
json bool

Whether to include error details as JSON. Defaults to False.

False

Returns:

Name Type Description
str str

The formatted error message.

InvalidIPAddress

Bases: IPNetworkError

Entity is not a valid IP address.

InvalidIPv4Address

Bases: IPNetworkError

Entity is not a valid IPv4 address.

InvalidIPv6Address

Bases: IPNetworkError

Entity is not a valid IPv6 address.

InvalidNetwork

Bases: IPNetworkError

Entity is not a valid network.

LoginFailedError

Bases: APIError

Login failed.

detail cached property

detail: str

Clean human-readable error message from the response.

Joins the detail of each parsed error with "; ". Empty if the response has no parseable errors.

details property

details: str

Get the error details from the response.

details_json property

details_json: str

Get the error details from the response.

errors cached property

Get the parsed MREG errors from the response.

Returns:

Type Description
MREGErrorResponse

The MREGErrorResponse object, or a default MREGErrorResponse with type "unknown" if the response

request property

request: Request | None

Get the request that triggered the exception.

Uses the request from the cause if not set directly.

Returns:

Type Description
Request | None

The request object or None if not set.

response property

response: Response | None

Get the response object associated with the error.

Uses the response from the cause if not set directly.

Returns:

Type Description
Response | None

The response object or None if not set.

status_code property

status_code: int | None

Get the HTTP status code of the response, if available.

__init__

__init__(
    message: str = "", response: Response | None = None
)

Initialize an APIError exception.

Parameters:

Name Type Description Default
message str

An optional message override. When empty, the message is derived from the response (parsed error details, then raw text, then reason phrase) via formatted_message.

''
response Response | None

The response object that triggered the exception.

None

__str__

__str__() -> str

Return the full formatted error message (request info + details).

formatted_message

formatted_message(*, json: bool = False) -> str

Get a formatted error message including error details.

Parameters:

Name Type Description Default
json bool

Whether to include error details as JSON. Defaults to False.

False

Returns:

Name Type Description
str str

The formatted error message.

MREGError pydantic-model

Bases: BaseModel

Details of an MREG error.

Show JSON schema:
{
  "description": "Details of an MREG error.",
  "properties": {
    "code": {
      "title": "Code",
      "type": "string"
    },
    "detail": {
      "title": "Detail",
      "type": "string"
    },
    "attr": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "title": "Attr"
    }
  },
  "required": [
    "code",
    "detail",
    "attr"
  ],
  "title": "MREGError",
  "type": "object"
}

Fields:

attr pydantic-field

attr: str | None

The attribute (field) associated with the error, if any.

code pydantic-field

code: str

The error code identifying the type of error.

detail pydantic-field

detail: str

Human-readable representation of the error.

fmt_error

fmt_error() -> str

Format the error message.

Returns:

Type Description
str

A formatted error message.

MREGErrorResponse pydantic-model

Bases: BaseModel

MREG error response.

Show JSON schema:
{
  "$defs": {
    "MREGError": {
      "description": "Details of an MREG error.",
      "properties": {
        "code": {
          "title": "Code",
          "type": "string"
        },
        "detail": {
          "title": "Detail",
          "type": "string"
        },
        "attr": {
          "anyOf": [
            {
              "type": "string"
            },
            {
              "type": "null"
            }
          ],
          "title": "Attr"
        }
      },
      "required": [
        "code",
        "detail",
        "attr"
      ],
      "title": "MREGError",
      "type": "object"
    }
  },
  "description": "MREG error response.",
  "properties": {
    "type": {
      "title": "Type",
      "type": "string"
    },
    "errors": {
      "default": [],
      "items": {
        "$ref": "#/$defs/MREGError"
      },
      "title": "Errors",
      "type": "array"
    }
  },
  "required": [
    "type"
  ],
  "title": "MREGErrorResponse",
  "type": "object"
}

Fields:

detail cached property

detail: str

Get the detail field of the error(s).

Most MREG error responses only contain a single error object.

__bool__

__bool__() -> bool

Response contains MREGError objects if True, else False.

as_json_str

as_json_str(indent: int = 2) -> str

Convert the error response to a JSON string.

Parameters:

Name Type Description Default
indent int

The indentation level for the JSON string.

2

Returns:

Type Description
str

A JSON string representation of the error response.

as_str

as_str() -> str

Convert the error response to a string.

Returns:

Type Description
str

A string representation of the error response.

MregApiBaseError

Bases: Exception

Base exception class for MREG API exceptions.

MregValidationError

Bases: MregApiBaseError

Validation error class for MREG API.

Stems from Pydantic ValidationError but adds context about the API request that caused the validation to fail.

__init__

__init__(
    message: str,
    pydantic_error: ValidationError | None = None,
)

Initialize an MregValidationError.

Parameters:

Name Type Description Default
message str

The error message.

required
pydantic_error ValidationError | None

The Pydantic validation error, if any.

None

from_pydantic classmethod

from_pydantic(
    e: ValidationError, context: str | None = None
) -> MregValidationError

Create an MregValidationError from a Pydantic MregValidationError.

Parameters:

Name Type Description Default
e ValidationError

The Pydantic MregValidationError.

required
context str | None

What was being validated (e.g., "JSON", "string", "object").

None

Returns:

Type Description
MregValidationError

The created MregValidationError.

MultipleEntitiesFound

Bases: MregApiBaseError

Multiple entities found when only one was expected.

PatchError

Bases: APIError

Error class for failed patching.

detail cached property

detail: str

Clean human-readable error message from the response.

Joins the detail of each parsed error with "; ". Empty if the response has no parseable errors.

details property

details: str

Get the error details from the response.

details_json property

details_json: str

Get the error details from the response.

errors cached property

Get the parsed MREG errors from the response.

Returns:

Type Description
MREGErrorResponse

The MREGErrorResponse object, or a default MREGErrorResponse with type "unknown" if the response

request property

request: Request | None

Get the request that triggered the exception.

Uses the request from the cause if not set directly.

Returns:

Type Description
Request | None

The request object or None if not set.

response property

response: Response | None

Get the response object associated with the error.

Uses the response from the cause if not set directly.

Returns:

Type Description
Response | None

The response object or None if not set.

status_code property

status_code: int | None

Get the HTTP status code of the response, if available.

__init__

__init__(
    message: str = "", response: Response | None = None
)

Initialize an APIError exception.

Parameters:

Name Type Description Default
message str

An optional message override. When empty, the message is derived from the response (parsed error details, then raw text, then reason phrase) via formatted_message.

''
response Response | None

The response object that triggered the exception.

None

__str__

__str__() -> str

Return the full formatted error message (request info + details).

formatted_message

formatted_message(*, json: bool = False) -> str

Get a formatted error message including error details.

Parameters:

Name Type Description Default
json bool

Whether to include error details as JSON. Defaults to False.

False

Returns:

Name Type Description
str str

The formatted error message.

PostError

Bases: APIError

Error class for failed creation.

detail cached property

detail: str

Clean human-readable error message from the response.

Joins the detail of each parsed error with "; ". Empty if the response has no parseable errors.

details property

details: str

Get the error details from the response.

details_json property

details_json: str

Get the error details from the response.

errors cached property

Get the parsed MREG errors from the response.

Returns:

Type Description
MREGErrorResponse

The MREGErrorResponse object, or a default MREGErrorResponse with type "unknown" if the response

request property

request: Request | None

Get the request that triggered the exception.

Uses the request from the cause if not set directly.

Returns:

Type Description
Request | None

The request object or None if not set.

response property

response: Response | None

Get the response object associated with the error.

Uses the response from the cause if not set directly.

Returns:

Type Description
Response | None

The response object or None if not set.

status_code property

status_code: int | None

Get the HTTP status code of the response, if available.

__init__

__init__(
    message: str = "", response: Response | None = None
)

Initialize an APIError exception.

Parameters:

Name Type Description Default
message str

An optional message override. When empty, the message is derived from the response (parsed error details, then raw text, then reason phrase) via formatted_message.

''
response Response | None

The response object that triggered the exception.

None

__str__

__str__() -> str

Return the full formatted error message (request info + details).

formatted_message

formatted_message(*, json: bool = False) -> str

Get a formatted error message including error details.

Parameters:

Name Type Description Default
json bool

Whether to include error details as JSON. Defaults to False.

False

Returns:

Name Type Description
str str

The formatted error message.

TooManyResults

Bases: MregApiBaseError

API returned too many results.

UnexpectedDataError

Bases: APIError

Error class for unexpected API data.

detail cached property

detail: str

Clean human-readable error message from the response.

Joins the detail of each parsed error with "; ". Empty if the response has no parseable errors.

details property

details: str

Get the error details from the response.

details_json property

details_json: str

Get the error details from the response.

errors cached property

Get the parsed MREG errors from the response.

Returns:

Type Description
MREGErrorResponse

The MREGErrorResponse object, or a default MREGErrorResponse with type "unknown" if the response

request property

request: Request | None

Get the request that triggered the exception.

Uses the request from the cause if not set directly.

Returns:

Type Description
Request | None

The request object or None if not set.

response property

response: Response | None

Get the response object associated with the error.

Uses the response from the cause if not set directly.

Returns:

Type Description
Response | None

The response object or None if not set.

status_code property

status_code: int | None

Get the HTTP status code of the response, if available.

__init__

__init__(
    message: str = "", response: Response | None = None
)

Initialize an APIError exception.

Parameters:

Name Type Description Default
message str

An optional message override. When empty, the message is derived from the response (parsed error details, then raw text, then reason phrase) via formatted_message.

''
response Response | None

The response object that triggered the exception.

None

__str__

__str__() -> str

Return the full formatted error message (request info + details).

formatted_message

formatted_message(*, json: bool = False) -> str

Get a formatted error message including error details.

Parameters:

Name Type Description Default
json bool

Whether to include error details as JSON. Defaults to False.

False

Returns:

Name Type Description
str str

The formatted error message.

determine_http_error_class

determine_http_error_class(method: str) -> type[APIError]

Get the appropriate exception class for a given HTTP method.

Parameters:

Name Type Description Default
method str

The HTTP method.

required

Returns:

Type Description
type[APIError]

The exception class corresponding to the HTTP method.

fmt_error_code

fmt_error_code(code: str) -> str

Format the error code.

Parameters:

Name Type Description Default
code str

The error code to format.

required

Returns:

Type Description
str

The formatted error code.

parse_mreg_error

parse_mreg_error(
    resp: Response,
) -> MREGErrorResponse | None

Parse an MREG error response.

Parameters:

Name Type Description Default
resp Response

The response object to parse.

required

Returns:

Type Description
MREGErrorResponse | None

A MREGErrorResponse object or None if it cannot be parsed.