Skip to content

ErrorDetail cannot be rendered to yaml #4

@jobec

Description

@jobec

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions