Skip to content

Commit

Permalink
Handle SimpleLazyObject in payloads (#297)
Browse files Browse the repository at this point in the history
* This needs some cleanup, but the basic idea is to handle SimpleLazyObject via a custom JSONEncoder subclass

* Use a function instead of a subclass

* Return a string instead of throwing a TypeError
  • Loading branch information
rokob committed Dec 18, 2018
1 parent c1bc132 commit b9caf5d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions rollbar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import requests
import six

from rollbar.lib import events, filters, dict_merge, parse_qs, text, transport, urljoin, iteritems
from rollbar.lib import events, filters, dict_merge, parse_qs, text, transport, urljoin, iteritems, defaultJSONEncode


__version__ = '0.14.5'
__log_name__ = 'rollbar'
Expand Down Expand Up @@ -1328,7 +1329,7 @@ def _build_payload(data):


def _serialize_payload(payload):
return json.dumps(payload)
return json.dumps(payload, default=defaultJSONEncode)


def _send_payload(payload_str, access_token):
Expand Down
14 changes: 14 additions & 0 deletions rollbar/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import sys
from array import array
import json

try:
# Python 3
Expand Down Expand Up @@ -212,3 +213,16 @@ def unencodable_object_label(data):
def undecodable_object_label(data):
return '<Undecodable type:(%s) base64:(%s)>' % (type(data).__name__,
base64.b64encode(data).decode('ascii'))

try:
from django.utils.functional import SimpleLazyObject
except ImportError:
SimpleLazyObject = None


def defaultJSONEncode(o):
if SimpleLazyObject and isinstance(o, SimpleLazyObject):
if not o._wrapped:
o._setup()
return o._wrapped
return repr(o) + " is not JSON serializable"

0 comments on commit b9caf5d

Please sign in to comment.