Skip to content

return ExecutionError if database execution fails

Benedikt Neuffer requested to merge better-error-handling into master

Currently a Value Error with a text message is returned if database execution fails. It is not possible to easily access the returned error_type and error_code from API.

This merge request adds a new ExecutionError with satus_code, reason and result attribute.

  • status_code is the HTTP status code returned by requests.
  • reason is the human readable reason for the status_code
  • result is the returned data from the API, text or dict

ExecutionError is derrivated from ValueError to keep backward compatibilty.

This change allows software which uses netdb-client-lib to access the status_code , reason and returned result from API.

The code is tested for backwards compatibility and no problems were seen.

This is an example code how the new error handling can be used:

from netdb_client import APIEndpoint, APISession, ExecutionError
endpoint = APIEndpoint('www-net.scc.kit.edu', token=token)
api = APISession(endpoint)
new_ta = ['']
except ExecutionError as e:
    print(e)
    print(e.status_code)
    print(e.reason)
    print(e.result)
    if isinstance(e.result, dict):
        if e.result['exception']['error']['code'] == 4 and \
                e.result['exception']['error_type']['code'] == -20900:
            print('API doesn\'t like the request.')
    else:
        print(f"API returned the following error message: {e.result}")
Edited by Benedikt Neuffer

Merge request reports

Loading