Skip to content

Error handling

For the complete documentation index see: llms.txt

All documentation pages available in markdown.

On error, the Aerospike Python client raises an AerospikeError exception or one of its subclasses. The exception includes the following attributes:

AttributeDescription
codeThe error code that identifies the error. See as_status.h of the Aerospike C client for error code descriptions.
msgThe error message with details.
fileThe file where the error occurred.
lineThe line number in the file where the error occurred.

Exception Handling

To handle the exceptions, wrap the code in a try-except block:

from __future__ import print_function
import aerospike
from aerospike.exception import *
try:
config = { 'hosts': [ ('127.0.0.1', 3000)], 'policies': { 'timeout': 1200}}
client = aerospike.client(config)
client.close()
except ClientError as e:
print("Error: {0} [{1}]".format(e.msg, e.code))

Server error details

Aerospike Database 8.2.0 can return a numeric subcode and a message on failed operations. Enable this by setting error_detail_verbosity in the policy dict passed to the operation, then read the subcode from the exception:

import aerospike
policy = {"error_detail_verbosity": aerospike.ERROR_DETAIL_SUBCODE}
try:
(key, meta, bins) = client.get(key, policy)
except aerospike.exception.AerospikeError as e:
print(f"status={e.code} subcode={e.subcode} message={e.msg}")

Verbosity levels are named constants on the aerospike module:

ValueConstantServer returns
0aerospike.ERROR_DETAIL_NONETop-level status only (default)
1aerospike.ERROR_DETAIL_SUBCODESubcode
2aerospike.ERROR_DETAIL_MESSAGESubcode and message
33Subcode, message, and expression trace
  • AerospikeError.subcode is the numeric subcode, or aerospike.SUB_NONE when the server didn’t return one. Dispatch on the (code, subcode) pair, never the subcode alone. Constants are aerospike.SUB_* (for example aerospike.SUB_FORBID_TRUNCATED). See Error subcodes.
  • AerospikeError.msg includes the server’s detail text at verbosity 2.

The Python client does not decode the verbosity-3 expression trace into a data structure, but it does return a string of the expression trace in the msg field. To debug expression failures with a trace, use the Java or Go client.

See Server error details for behavior, operator caps, and coverage, and the Client Compatibility Matrix for the Python client version that pairs with Database 8.2.0.