django-rest-framework-yaml
django-rest-framework-yaml copied to clipboard
ErrorDetail cannot be rendered to yaml
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)
@jpadilla Is this package abandoned?
Hey @jobec not particularly, just been busy. Could use some help maintaining this package if you've got some free cycles. Up for it?
I tried upgrading to restframework newer then 3.5.0.
would be nice to have a new package to fix this error!
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.
I’m afraid this repo is abandoned...
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.