django-rest-framework-yaml icon indicating copy to clipboard operation
django-rest-framework-yaml copied to clipboard

ErrorDetail cannot be rendered to yaml

Open jobec opened this issue 7 years ago • 6 comments

When you raise a NotFound exception in django rest framework, the YAML renderer triggers an internal server error. So when you do the following in a view:

raise exceptions.NotFound("some error message")

It will raise this error:

...
  File "virtualenv/lib/python3.4/site-packages/rest_framework_yaml/encoders.py", line 38, in represent_mapping
    node_value = self.represent_data(item_value)
  File "virtualenv/lib/python3.4/site-packages/yaml/representer.py", line 57, in represent_data
    node = self.yaml_representers[None](self, data)
  File "virtualenv/lib/python3.4/site-packages/yaml/representer.py", line 229, in represent_undefined
    raise RepresenterError("cannot represent an object: %s" % data)
yaml.representer.RepresenterError: cannot represent an object: some error message

It's because the error detail of an APIException is turned into an ErrorDetail instance which is a str subclass. PyYaml doesn't recognize this and fails. This can be fixed by adding a representer.

from rest_framework import exceptions
from rest_framework_yaml.encoders import SafeDumper
from yaml.representer import SafeRepresenter

SafeDumper.add_representer(exceptions.ErrorDetail, SafeRepresenter.represent_str)

jobec avatar Mar 20 '17 14:03 jobec

@jpadilla Is this package abandoned?

jobec avatar Aug 03 '17 12:08 jobec

Hey @jobec not particularly, just been busy. Could use some help maintaining this package if you've got some free cycles. Up for it?

jpadilla avatar Aug 03 '17 17:08 jpadilla

I tried upgrading to restframework newer then 3.5.0.

would be nice to have a new package to fix this error!

TZanke avatar Jun 18 '18 10:06 TZanke

Hey @jpadilla it's been over 2 years now since the fix is out. It would be great if you could merge the fix and create a new package.

tidenhub avatar Mar 28 '19 11:03 tidenhub

I’m afraid this repo is abandoned...

jobec avatar Mar 28 '19 16:03 jobec

I could use some help updating versions like in https://github.com/jpadilla/django-rest-framework-xml/pull/32.

Tests on #5 were failing for an unrelated reason, but with some help to make sure we're testing right versions, we can re-run tests on #5, merge that, and release a new version.

jpadilla avatar Mar 28 '19 21:03 jpadilla