Skip to content

Missing "errors" field in error response #218

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

Closed
joeyespo opened this issue Mar 29, 2016 · 4 comments
Closed

Missing "errors" field in error response #218

joeyespo opened this issue Mar 29, 2016 · 4 comments

Comments

@joeyespo
Copy link
Contributor

The 400-level errors are responding with a JSON array containing error objects, but the JSON API spec requires a top-level error field.

To reproduce
  1. Use serializer.is_valid(raise_exception=True) in an API view

  2. Send invalid JSON from the client to any API endpoint

    Example:

    ajax({
      url: '/my/api',
      contentType: 'application/vnd.api+json',
      type: 'POST',
      data: 'not JSON'
    });

    Expected response:

    {
      "errors": [
        {
          "status": "400",
          "source": {"pointer": "/data"},
          "detail": "JSON parse error - No JSON object could be decoded"
        }
      ]
    }

    Actual response:

      [
        {
          "status": "400",
          "source": {"pointer": "/data"},
          "detail": "JSON parse error - No JSON object could be decoded"
        }
      ]

It seems like the exception handler is properly setting the resource name, which should be used here. The former gets called, but the latter does not. DRF seems to have taken over, responding with this project's exception_handler as-is.

My requirements
...
Django==1.9.1
djangorestframework==3.3.3
djangorestframework-jsonapi==2.0.0-beta.2
...
My config
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework_json_api.renderers.JSONRenderer',
    ),
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework_json_api.parsers.JSONParser',
        'rest_framework.parsers.FormParser',
        'rest_framework.parsers.MultiPartParser',
    ),
    'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
}
@joeyespo joeyespo changed the title Missing errors in error response Missing "errors" field in error response Mar 29, 2016
@jerel
Copy link
Member

jerel commented Apr 11, 2016

@joeyespo thanks for the detailed bug report. Did you end up finding a fix and would you like to open a PR? If not I'll try to get to it myself. I'm guessing that the issue has something to do with calling serializer.is_valid(raise_exception=True) manually rather than by the view itself.

@joeyespo
Copy link
Contributor Author

@jerel Thanks for the reply! I did. I got something working locally for both this and #214. I'll open a PR within the next couple days.

@jerel
Copy link
Member

jerel commented Apr 14, 2016

@joeyespo good to hear! I did just merge a pull request (#222) that moves the error formatting to utils.py. Not much changed with the implementation but you'll want to pull that in before opening your PR

@joeyespo
Copy link
Contributor Author

Ok. Rebased and opening the PR now. Feel free to rename any variables or completely change it.

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

2 participants