Bug 50249 - Improve error response format
Improve error response format
Status: CLOSED FIXED
Product: UCS
Classification: Unclassified
Component: UDM - REST API
UCS 4.4
Other Linux
: P5 normal (vote)
: UCS 5.0-2-errata
Assigned To: Florian Best
Peter Stoll
https://git.knut.univention.de/univen...
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2019-09-23 17:40 CEST by Florian Best
Modified: 2022-12-14 15:04 CET (History)
3 users (show)

See Also:
What kind of report is it?: Development Internal
What type of bug is this?: ---
Who will be affected by this bug?: ---
How will those affected feel about the bug?: ---
User Pain:
Enterprise Customer affected?:
School Customer affected?:
ISV affected?:
Waiting Support:
Flags outvoted (downgraded) after PO Review:
Ticket number:
Bug group (optional): Cleanup
Max CVSS v3 score:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Florian Best univentionstaff 2019-09-23 17:40:08 CEST
The error response format of the UDM REST API is currently like the following:

# http --auth Administrator:univention ':/univention/udm/users/user/?scope=two' Accept:application/json
{
    "error": {
        "code": 422,
        "error": {
            "query_string": {
                "scope": "Value has to be one of [u'sub', u'one', u'base', u'base+one']"
            }
        },
        "message": "1 error(s) occurred:\nQuery string \"scope\": Value has to be one of [u'sub', u'one', u'base', u'base+one']\n",
        "title": "Unprocessable Entity",
        "traceback": null
    }
}

We could improove this. Ideas:
* use a HAL embedded object, e.g. "udm:error" instead of "error".
* cleanup message?
* each item in "error" should be described and errors in properties should be indented/nested so that the structure of looks like:

{
    "code": 422,
    "title": "Unprocessable Entity",
    "traceback": null
    "_embedded": {
        "udm:error": [{
            "properties": {
                  "username": "The username is prohibited...",
                  "uidNumber": "The uid is alread in use",
             },
# ?      }, {
            "options": {
                 "foo": "unknown option",
             }
        }]
    }
}
(just a draft, needs a little bit more thinking)
Comment 1 Florian Best univentionstaff 2022-12-08 13:00:32 CET
The error response format has been improved while being backwards compatible in the response format.

Previously `response['error']['error']` contained a inconsistent sometimes nested dict structure.
The new format contains a embedded HAL relation/object which allows multiple errors as list describing their location, e.g.:
```
"udm:error": [
    {   
        "location": [
            "query",
            "position"
        ],  
        "message": "Value is not a LDAP DN.",
        "type": "value_error"
    }   
]
```

That format is also now also described by the OpenAPI schema.

A new test case which tests various errors has been added.

Some example requests to trigger errors:
eval "$(ucr shell ldap/base)"
curl -H 'Accept: application/json' "http://Administrator:univention@localhost/univention/udm/users/user/uid=Administrator,cn=users,$ldap_base" -X PUT -H 'Content-Type: application/json' -d '{"properties": {}}' | python -m json.tool
curl -H 'Accept: application/json' 'http://Administrator:univention@localhost/univention/udm/users/user/?scope=blah&filter=foobar' | python -m json.tool
curl -H 'Accept: application/json' "http://Administrator:univention@localhost/univention/udm/users/user/uid=Administrator,cn=users,$ldap_base" -X PATCH -H 'Content-Type: application/json' -d '{"properties": {"description": ["foo"], "lastname": []}}' | python -m json.tool
curl -s -H 'Accept: application/json' "http://Administrator:univention@localhost/univention/udm/users/user/?position=cn=userz,$ldap_base" | python -m json.tool


univention-directory-manager-rest.yaml
88ce48c30d80 | feat(udm-rest): enhance response error format using HAL

univention-directory-manager-rest (10.0.4-9)
a7fb5930d72e | feat(udm-rest-client): add error handling for 422 and 500 errors
041a8521a773 | refactor(udm-rest): use typing for argument sanitization

univention-directory-manager-rest (10.0.4-10)
88ce48c30d80 | feat(udm-rest): enhance response error format using HAL

ucs-test (10.0.7-29)
88ce48c30d80 | feat(udm-rest): enhance response error format using HAL
Comment 2 Peter Stoll univentionstaff 2022-12-08 17:34:46 CET
Verified:

* code review
* changelogs and YAML OK
* compared differences of OpenAPI schema
* successfully executing ucs-test/73_udm-rest
* install of official package and checked completness