Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ErrorDetail cannot be rendered to yaml #4

Open
jobec opened this issue Mar 20, 2017 · 6 comments
Open

ErrorDetail cannot be rendered to yaml #4

jobec opened this issue Mar 20, 2017 · 6 comments

Comments

@jobec
Copy link

jobec commented Mar 20, 2017

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 added a commit to jobec/django-rest-framework-yaml that referenced this issue Mar 20, 2017
@jobec
Copy link
Author

jobec commented Aug 3, 2017

@jpadilla Is this package abandoned?

@jpadilla
Copy link
Owner

jpadilla commented Aug 3, 2017

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

@TZanke
Copy link

TZanke commented Jun 18, 2018

I tried upgrading to restframework newer then 3.5.0.

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

@tidenhub
Copy link

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.

@jobec
Copy link
Author

jobec commented Mar 28, 2019

I’m afraid this repo is abandoned...

@jpadilla
Copy link
Owner

I could use some help updating versions like in jpadilla/django-rest-framework-xml#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.

stefan6419846 added a commit to stefan6419846/django-rest-framework-yaml that referenced this issue Jun 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants