|
| 1 | +##Exceptions |
| 2 | + |
| 3 | +When you make a request via the SDK, there is a chance of request failing |
| 4 | +due to various reasons. When such a failure happens, an exception |
| 5 | +corresponding to the error occurred is raised. |
| 6 | + |
| 7 | + |
| 8 | +---- |
| 9 | +####Possible Exceptions |
| 10 | + |
| 11 | +* `BadRequestException` If the request returns with status code `400` |
| 12 | +* `UnauthorizedException` If the request returns with status code `401` |
| 13 | +* `ForbiddenException` If the request returns with status code `403` |
| 14 | +* `NotFoundException` If the request returns with status code `404` |
| 15 | +* `MethodNotAllowedException` If the request returns with status code `405` |
| 16 | +* `TooManyRequestsException` If the request returns with status code `429` |
| 17 | +* `PleaseContactBunqException` If the request returns with status code `500`. |
| 18 | +If you get this exception, please contact us preferably via the support chat in the bunq app. |
| 19 | +* `UnknownApiErrorException` If none of the above mentioned exceptions are raised, |
| 20 | +this exception will be raised instead. |
| 21 | + |
| 22 | +For more information regarding these errors, please take a look on the documentation |
| 23 | +page here: https://doc.bunq.com/api/1/page/errors |
| 24 | + |
| 25 | +--- |
| 26 | +####Base exception |
| 27 | +All the exceptions have the same base exception which looks like this: |
| 28 | +```python |
| 29 | +class ApiException(Exception): |
| 30 | + def __init__(self, message, response_code): |
| 31 | + pass |
| 32 | + |
| 33 | + @property |
| 34 | + def message(self): |
| 35 | + """ |
| 36 | + :rtype: str |
| 37 | + """ |
| 38 | + |
| 39 | + return self._message |
| 40 | + |
| 41 | + @property |
| 42 | + def response_code(self): |
| 43 | + """ |
| 44 | + :rtype: int |
| 45 | + """ |
| 46 | + |
| 47 | + return self._response_code |
| 48 | +``` |
| 49 | +This means that each exception will have the response code and the error message |
| 50 | +related to the specific exception that has been raised. |
| 51 | + |
| 52 | +--- |
| 53 | +####Exception handling |
| 54 | +Because we raise different exceptions for each error, you can catch an error |
| 55 | +if you expect it to be raised. |
| 56 | + |
| 57 | +```python |
| 58 | +from bunq.sdk.exception import BadRequestException |
| 59 | +from bunq.sdk.context import ApiEnvironmentType, ApiContext |
| 60 | + |
| 61 | +API_KEY = "Some invalid API key" |
| 62 | +DESCRIPTION = "This wil raise a BadRequestException" |
| 63 | + |
| 64 | +try: |
| 65 | + # Make a call that might raise an exception |
| 66 | + ApiContext(ApiEnvironmentType.SANDBOX, API_KEY, DESCRIPTION) |
| 67 | +except BadRequestException as error: |
| 68 | + # Do something if exception is raised |
| 69 | + print(error.response_code) |
| 70 | + print(error.message) # or just print(error) |
| 71 | +``` |
| 72 | + |
| 73 | +This will ensure that you are ready for anything that might go wrong! |
0 commit comments