Error handling

All methods from proxy classes return Munch with data only when the API call succeeds. Otherwise, an exception is raised.

This example code tries to cancel a build. Such thing is possible only when the build is not already finished.

from copr.v3 import Client
client = Client.create_from_config_file()

try:
    build = client.build_proxy.cancel(123)
    print("Build {} is {}".format(build.id, build.state))
except CoprRequestException as ex:
    print(ex)

In case that the build can be canceled, we get this output.

Build 123 is canceled

Otherwise, an exception is raised and handled.

Cannot cancel build 123

Debugging

Sometimes it is useful to dig deeper and examine the failure. Exceptions contain a result attribute returning a Munch with additional information.

except CoprRequestException as ex:
    print(ex)
    print(ex.result)
Cannot cancel build 123
Munch({'__response__': <Response [500]>, 'error': u'Cannot cancel build 123'})

The stored response is a standard requests.Reponse so every possible detail like status code or headers can be examined.

except CoprRequestException as ex:
    print(type(ex.result.__response__))
    print(ex.result.__response__)
    print(ex.result.__response__.status_code)
    print(ex.result.__response__.headers)
<class 'requests.models.Response'>
<Response [500]>
500
{'Date': 'Wed, 25 Jul 2018 21:40:48 GMT', 'Content-Length': '42', 'Content-Type': 'application/json', 'Server': 'Werkzeug/0.12.2 Python/3.6.4'}

Exception hierarchy

exception copr.v3.exceptions.CoprException(msg=None, response=None)[source]

Bases: exceptions.Exception

Base Copr exception

exception copr.v3.exceptions.CoprRequestException(msg=None, response=None)[source]

Bases: copr.v3.exceptions.CoprException

Raised when the API request doesn’t proceed successfully

exception copr.v3.exceptions.CoprNoResultException(msg=None, response=None)[source]

Bases: copr.v3.exceptions.CoprException

Raised when no result data is returned

exception copr.v3.exceptions.CoprValidationException(msg=None, response=None)[source]

Bases: copr.v3.exceptions.CoprException

Raised when the data sent from client to API are not valid

exception copr.v3.exceptions.CoprNoConfigException(msg=None, response=None)[source]

Bases: copr.v3.exceptions.CoprException

Exception thrown when no config file is found

exception copr.v3.exceptions.CoprConfigException(msg=None, response=None)[source]

Bases: copr.v3.exceptions.CoprException

Exception thrown when the config file is incomplete or malformed.