Skip to content

ContentNotRenderedError when JSON exception swallowed #6364

@rrauenza

Description

@rrauenza

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • [ x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [ x] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [x ] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • [ x] I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

I've tested this in 3.8.2

Also posted here for future searches: https://stackoverflow.com/questions/53842591/contentnotrenderederror-in-django-rest-framework

I've built a view and serializer where the serializer has a field that is a SerializerMethodField(). In that field's get method I have something like:

def get_result(self, obj):
    result = Result(obj.a, obj.b, obj.c)
    return dict(
        percent_change=result.percent_change,
        [...]
    )

Turns out my result.percent_change sometimes was NaN or Infinity which is not supported by JSON.

The problem is that this root cause was difficult to find because the JSON exception was somehow swallowed and we ended up with a ContentNotRenderedError: The response content must be rendered before it can be accessed. instead.

Regardless whether this gets fixed or not, wanted to post this so people searching for this error realize it's likely a swallowed exception.

Related? #4809

Expected behavior

Instead of ContentNotRenderedError exception, it would be nice to see original exception somehow.

(Or some advice how to find the root cause exception.)

Actual behavior

JSON conversion exception is swallowed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions