Troubleshooting Python SDK
How to find the version of your SDK
You can access your SDK version via constant
import os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
def main():
# Display the current SDK version
print(f"Current PubNub SDK Version: {PubNub.SDK_VERSION}")
# Optional: You can also initialize a PubNub instance and check the version
pn_config = PNConfiguration()
pn_config.subscribe_key = os.getenv('SUBSCRIBE_KEY', 'demo') # Replace 'demo' with your subscribe key from the PubNub Admin Portal
pn_config.user_id = os.getenv('USER_ID', 'my_custom_user_id')
pubnub = PubNub(pn_config)
print(f"Initialized PubNub with SDK Version: {pubnub.SDK_VERSION}")
show all 18 linesError handling with sync()
In case of sync()
calls errors will be wrapped out by either Envelope
or PubNubAsyncioException
which both implement two object fields:
-
e.result - a request result object in case of success, otherwise
None
-
e.status -
PNStatus
object with useful information about the finished request. You may check if it was an error or a success usinge.is_error()
helper.
sync() usage
import os
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.exceptions import PubNubException
def publish_message(pubnub: PubNub):
try:
envelope = pubnub.publish() \
.channel("my_channel") \
.message("hello!") \
.sync()
print(f"Published message with timetoken: {envelope.result.timetoken}")
print(f"Full result: {str(envelope.result)}")
except PubNubException as e:
print(f"Error: {str(e)}")
show all 32 linesError handling with async()
async()
call lets you check for errors using status.is_error()
helper:
import os
import time
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
from pubnub.callbacks import SubscribeCallback
from pubnub.enums import PNStatusCategory, PNOperationType
class MySubscribeCallback(SubscribeCallback):
def status(self, pubnub, status):
if status.is_error():
print(f"Error: {str(status.error_data.exception)}")
print(f"Error category: {status.category}")
elif status.category == PNStatusCategory.PNConnectedCategory:
print("Connected to PubNub!")
elif status.category == PNStatusCategory.PNReconnectedCategory:
show all 72 lines