netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.17k stars 2.59k forks source link

REST machine readable errors - RFC7807 #5119

Closed agowa closed 4 years ago

agowa commented 4 years ago

Environment

Proposed Functionality

Please add machine readable error messages, for example like defined by RFC7807

Use Case

Given the following API call

PUT /api/ipam/ip-addresses/27/?format=json HTTP/1.1
Host: netbox-lab
Authorization: token TokenValue
Content-Type: application/json

{
  "tenant": 2,
  "address": "192.168.178.10/24",
  "id": 27,
  "vrf": 2,
  "dns_name": "vm001.example.com",
  "status": "active",
  "custom_fields": {},
  "interface": 16,
  "tags": [
    "someTag"
  ]
}

the API currently returns this in case of an error:

HTTP/1.1 400 Bad Request
Date: Wed, 09 Sep 2020 16:44:15 GMT
Content-Type: application/json
Content-Length: 110
Connection: close
Server: gunicorn/20.0.4
Vary: Accept, Cookie, Origin
Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS
API-Version: 2.8
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN

{"interface":["IP address is primary for device SomeOtherDevicesName but assigned to None (Network adapter 1)"]}

This error message is hard to handle. It would be much easier if the API follows any standard like e.g. RFC7807.

(To reproduce the above error one just needs a 2nd VM that has the primary IP already assigned before one tries to assign it to another one).

Database Changes

none

External Dependencies

none

DanSheps commented 4 years ago

Can you actually propose a specific change? We aren't going to read through an RFC to extract examples.

agowa commented 4 years ago

There are examples in the rfc section 3: https://tools.ietf.org/html/rfc7807#section-3

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'"}
                  ]
}
jeremystretch commented 4 years ago

The validation errors are primarily a function of the Django REST Framework. This might be worth proposing to that project, but it's not something we can commit development resources to in NetBox.

agowa commented 4 years ago

From the DRF issue:

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. (...) We probably could add another error handler that would implement that RFC though and some 3rd party may already implement it

Such a 3rd party library is drf-problems. https://shivanshs9.me/medium/drf-problems-21f7bb4d4675 https://pypi.org/project/drf-problems/