Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pubnub/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class PNStatusCategory(Enum):
PNInternalExceptionCategory = 17
PNSubscriptionChangedCategory = 18
PNConnectionErrorCategory = 19
PNSerializationErrorCategory = 20


class PNOperationType(object):
Expand Down
12 changes: 10 additions & 2 deletions pubnub/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pubnub.models.consumer.common import PNStatus
from pubnub.errors import PNERR_JSON_NOT_SERIALIZABLE
from pubnub.exceptions import PubNubException
from pubnub.models.consumer.pn_error_data import PNErrorData


def get_data_for_user(data):
Expand All @@ -29,10 +30,17 @@ def get_data_for_user(data):
def write_value_as_string(data):
try:
return json.dumps(data)
except TypeError:
raise PubNubException(
except TypeError as e:
exc = PubNubException(
errormsg=str(e),
pn_error=PNERR_JSON_NOT_SERIALIZABLE
)
status = PNStatus()
status.category = PNStatusCategory.PNSerializationErrorCategory
status.error = True
status.error_data = PNErrorData(str(exc), exc)
exc.status = status
raise exc
Comment on lines +33 to +43
Copy link
Copy Markdown
Contributor Author

@jguz-pubnub jguz-pubnub Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error structure has a circular reference, PubNubException.status.error_data.exception points back to the same PubNubException. This PR implements a fix that works within the current model to prevent regressions, but the error hierarchy would benefit from being revisited in the future

Copy link
Copy Markdown
Member

@stephenlb stephenlb Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Good find and fast fix thank you 🎉

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick fix, I reported this to @stephenlb first at 2:48am IST and less than 24hrs it's fixed!

This was discovered by using a custom agent creating a tool on the fly in remote VPSs instance.



def url_encode(data):
Expand Down
33 changes: 33 additions & 0 deletions tests/integrational/asyncio/test_publish_serialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from datetime import datetime

import pytest

from pubnub.enums import PNStatusCategory
from pubnub.exceptions import PubNubAsyncioException
from pubnub.models.consumer.common import PNStatus
from pubnub.models.consumer.pn_error_data import PNErrorData
from pubnub.pubnub_asyncio import PubNubAsyncio
from tests.helper import pnconf_copy


@pytest.mark.asyncio
async def test_publish_non_serializable_returns_usable_error():
pubnub = PubNubAsyncio(pnconf_copy())

result = await pubnub.publish().channel("ch1").message({
"text": "Hello",
"timestamp": datetime.now(),
}).future()

assert isinstance(result, PubNubAsyncioException)
assert result.is_error() is True
assert isinstance(result.status, PNStatus)
assert result.status.error is True
assert result.status.category == PNStatusCategory.PNSerializationErrorCategory
assert isinstance(result.status.error_data, PNErrorData)
assert str(result) == (
"Trying to publish not JSON serializable object: "
"Object of type datetime is not JSON serializable"
)

await pubnub.stop()