encode / django-rest-framework

Web APIs for Django. 🎸
https://www.django-rest-framework.org
Other
28.18k stars 6.81k forks source link

Machine readable errors #7536

Closed agowa closed 4 years ago

agowa commented 4 years ago

Checklist

Steps to reproduce

Invoke the rest api with wrong parameters or attributes.

Expected behavior

RFC7807 compliant error reporting.

E.g. something like:

HTTP/1.1 400 Bad Request
Content-Type: application/problem+json
Content-Language: en

{
"type": "https://example.net/validation-error",
"title": "Your request parameters didn't validate.",
"invalid_params": [ {
                      "name": "age",
                      "reason": "must be a positive integer"
                    },
                    {
                      "name": "color",
                      "reason": "must be 'green', 'red' or 'blue'"}
                  ]
}

Actual behavior

Shows error messages that are hard to deal within scripts. And are only human readable.

An example for how the problem looks like in a real live scenario is available over at https://github.com/netbox-community/netbox/issues/5119. They sent me here, as the issue is something the framework does and not something they could implement/fix.

This issue also relates to:

xordoquy commented 4 years ago

the issue is something the framework does and not something they could implement/fix.

It would be more correct to say it is something they don't want to fix. You can change the default error representation by overriding the default error handler. Please note that RFC7807 is much younger than DRF and it is unlikely that we change the default error handler. We probably could add another error handler that would implement that RFC though and some 3rd party may already implement it - haven't looked. Meanwhile, as it is possible to override the default handler to implement RFC7807, I'm closing this issue.

agowa commented 4 years ago

Thanks for the clarification.

And for reference, there is a 3rd party library called drf-problems: https://shivanshs9.me/medium/drf-problems-21f7bb4d4675 https://pypi.org/project/drf-problems/

ChrisCrossCrash commented 3 years ago

@xordoquy Just so you know, I wanted the exact same thing as you. drf-problems works great, but it doesn't put the invalid parameters into an invalid-params property like the example given in the RFC7807 specification. I made a fork that does exactly that if you are interested.